I use useEffect with parameter props.quizStep.
But fn(keydown event listener function) cannot get current props.quizStep.
Why it cannot get current props?
Here is my code.
const moveBirdKeyboard = React.useEffect(() => {
console.log('moveBirdKeyboard props.quizStep', props.quizStep);
let fn = (e: any) => {
console.log('props.quizStep', props.quizStep);
moveBirdEvent(e);
}
if(eventListener.current === false) {
(window as any).addEventListener('keydown', _.throttle(fn,10)); //_.throttle(fn,1000));
eventListener.current = true;
}
}, [props.quizStep]);
You cannot assign the useEffect hook to a variable, as it says its a hook, not a property. Instead of that you need to create a function which properties can be attached to a hook.
A good example from the reactJS documentation:
function Example() {
const [count, setCount] = useState(0);
useEffect(() => {
document.title = `Clicked ${count} times`;
});