gpt4 book ai didi

react-hooks - 为什么 `react-hooks/exhaustive-deps` lint 规则会在嵌套对象属性上触发?

转载 作者:行者123 更新时间:2023-12-04 04:22:00 24 4
gpt4 key购买 nike

我正在尝试使用 React Hook 来内存回调。此回调专门使用在对象上定义的函数。

const setValue = useCallback((value) => {
field.setValue(key, value);
}, [field.setValue, key]);

这会触发 eslint 规则 react-hooks/exhaustive-deps .它希望我改为传递 [field, key] .

如果我随后将代码更改为以下代码,即使看起来等效,我也不会收到任何警告:
const { setValue: setFieldValue } = field;

const setValue = useCallback((value) => {
setFieldValue(key, value);
}, [setFieldValue, key]);

为什么 eslint 在第一个示例中警告我?在这种情况下我可以放心地忽略它吗?

最佳答案

试试这个。

const setValue = useCallback((value) => {
const set = field.setValue;
set(key, value);
}, [field.setValue, key]);

但不推荐。看看这个解释。 https://github.com/facebook/react/issues/15924#issuecomment-521253636

关于react-hooks - 为什么 `react-hooks/exhaustive-deps` lint 规则会在嵌套对象属性上触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58987583/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com