【发布时间】:2022-01-08 08:36:10
【问题描述】:
我正在寻找有关如何解决以下问题的建议:
我有一个组件 A,它有两个状态,state1 和 state2。
如果我只想在 state1 发生变化时运行效果,但在 useEFfect 回调中我使用 state2 的值,我该如何解决数组依赖 eslint react hooks 警告的问题?
例如
function A() {
const [state1, setState1] = useState(0);
const [state2, setState2] = useState(0);
useEffect(() => {
const a = state1 + state2;
// do some async logic using a
}, [state1]); //Here eslint react hooks rule requests adding state2 to the dependency array!
return <div> some text </div>
}
【问题讨论】:
标签: reactjs react-hooks use-effect