[英]Why does `react-hooks/exhaustive-deps` lint rule trigger on nested object properties?
I'm trying to use React hooks for memoizing a callback.我正在尝试使用 React 挂钩来记忆回调。 This callback specifically uses a function that's defined on an object.
此回调专门使用在 object 上定义的 function。
const setValue = useCallback((value) => {
field.setValue(key, value);
}, [field.setValue, key]);
This triggers Eslint rule react-hooks/exhaustive-deps
.这会触发 Eslint 规则
react-hooks/exhaustive-deps
。
It wants me to instead pass in [field, key]
.它希望我改为传入
[field, key]
。
If I then change the code to the following, I get no warning even though it seems equivalent:如果我随后将代码更改为以下代码,即使看起来等效,我也不会收到任何警告:
const { setValue: setFieldValue } = field;
const setValue = useCallback((value) => {
setFieldValue(key, value);
}, [setFieldValue, key]);
Why is Eslint warn me in the first example?为什么 Eslint 在第一个示例中警告我?
Can I safely ignore it in such circumstances?在这种情况下我可以放心地忽略它吗?
Try this.尝试这个。
const setValue = useCallback((value) => {
const set = field.setValue;
set(key, value);
}, [field.setValue, key]);
But it's not recommended.Take a look at this explanation.但不推荐。看看这个解释。 https://github.com/facebook/react/issues/15924#issuecomment-521253636
https://github.com/facebook/react/issues/15924#issuecomment-521253636
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.