【发布时间】:2019-05-21 02:54:27
【问题描述】:
我知道 useEffect 的依赖项是如何工作的。但是,在我的场景中,我不应该看到 prop 值的变化,我将其用作处理流程的条件,但如果我不将其放入依赖项数组中,我将收到 react-hooks/exhaustive-deps 警告。
我的情况是,如果 foo 和 fetchValue 发生变化,我想重新运行整个 fetch。虽然我使用 bar 作为条件来决定调用的是 fechValue,但在我的业务逻辑中,bar 的更改不应该重新运行块。
const Component = ({ foo, bar, fetchValue }) => {
useEffect = (
() => {
if(foo) {
if (bar) {
fetchValue();
}
}
},
[foo, fetchValue] // will get a warning `react-hooks/exhaustive-deps`
)
return <div />
}
【问题讨论】:
-
这听起来不像是一个有效的商业案例,更像是要求将来很难调试竞争条件 - 请您详细说明为什么需要这样做?
标签: reactjs react-hooks