【发布时间】:2021-02-18 11:21:05
【问题描述】:
考虑一下这个人为的代码 sn-p。
const foo = () => {return 1;}
const bar = useMemo(() => {return foo();}, [foo])
当我触发 react-hooks/exhaustive-deps 警告时,我会收到这样的消息。
The 'foo' function makes the dependencies of useMemo Hook (at line 2) change on every render. Move it inside the useMemo callback. Alternatively, wrap the definition of 'foo' in its own useCallback() Hook.
警告给了我 2 条建议:1) 将 foo 放在 bar 的 useMemo 回调中,或者 2) 将函数包装在 useCallback 中。
我当然也可以。我只是想知道:这两个选项中的任何一个是否比另一个更可取,如果是,那为什么?如果您的答案是“取决于”,那么它到底取决于什么?
【问题讨论】:
-
这可能是与 useEffect 的例子吗?
-
@NtshemboHlongwane 你可以得到与 useEffect 相同的警告,所以我认为这个问题也适用于 useEffect。
标签: reactjs react-hooks eslint