[英]Return undefined Redux store state in useEffect() function
My Redux store state return undefined in useEffect
hook.我的 Redux 商店 state 在
useEffect
挂钩中返回未定义。 If I remove empty array []
in useEffect
, my Redux store return true
value but very much and sometimes executed in an endless loop!如果我删除 useEffect 中的空数组
[]
,我的useEffect
存储返回true
值,但非常多,有时会在无限循环中执行!
useEffect(() => {
store.subscribe(() => {
setNodeProperties({
positionX: store.getState().schemaViewer.positionX,
positionY: store.getState().schemaViewer.positionY,
});
console.log(nodeProperties);
});
}, []);
The problem is setNodeProperties
will queue up the change for nodeProperties
and it won't happen right away because it is asynchronous.问题是
setNodeProperties
会将nodeProperties
的更改排队,并且不会立即发生,因为它是异步的。 So I would try to run a side effect once nodeProperties
is changing.因此,一旦
nodeProperties
发生变化,我会尝试产生副作用。 You can have multiple useEffect
hooks in your component.您的组件中可以有多个
useEffect
挂钩。
Try as the following:尝试如下:
useEffect(() => {
console.log(nodeProperties);
}, [nodeProperties]);
In this way you are capturing changes for nodeProperties
and run the logging step only then.通过这种方式,您将捕获
nodeProperties
的更改并仅在那时运行日志记录步骤。 Suggested read is Using the Effect Hook .建议阅读使用效果挂钩。
I hope this helps!我希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.