[英]can not get current props in useEffect
I use useEffect
with parameter props.quizStep
.我将
useEffect
与参数props.quizStep
一起使用。 But fn (keydown event listener function) cannot get current props.quizStep
.但是fn (keydown 事件监听函数)无法获取当前的
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.您不能将 useEffect 挂钩分配给变量,因为它说它是挂钩,而不是属性。 Instead of that you need to create a function which properties can be attached to a hook.
取而代之的是,您需要创建一个可以将属性附加到挂钩的函数。
A good example from the reactJS documentation: reactJS 文档中的一个很好的例子:
function Example() {
const [count, setCount] = useState(0);
useEffect(() => {
document.title = `Clicked ${count} times`;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.