简体   繁体   English

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

[英]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.

相关问题 是否有一个带有useEffect的无限滚动的实现,它不会触发react-hooks / exhaustive-deps lint警告? - Is there an implementation of infinite scroll with useEffect that doesn't trigger the react-hooks/exhaustive-deps lint warning? 未找到规则“re​​act-hooks/exhaustive-deps”的定义 - Definition for rule 'react-hooks/exhaustive-deps' was not found 了解 React Hooks 'exhaustive-deps' lint 规则 - Understanding the React Hooks 'exhaustive-deps' lint rule typescript es-lint 错误:react-hooks/exhaustive-deps - typescript es-lint error: react-hooks/exhaustive-deps React Hooks react-hooks/exhaustive-deps eslint 规则似乎过于敏感 - React Hooks react-hooks/exhaustive-deps eslint rule seems overly sensitive 如何在React中使用钩子实现componentDidMount以使其符合EsLint规则“ react-hooks / exhaustive-deps”:“ warn”? - How to implement componentDidMount with hooks in React to be in line with the EsLint rule “react-hooks/exhaustive-deps”: “warn”? 使用 i18next 时如何处理 ESLint react-hooks 'exhaustive-deps' 规则? - How to handle ESLint react-hooks 'exhaustive-deps' rule when using i18next? 反应 useEffect 带有警告的钩子 react-hooks/exhaustive-deps - React useEffect hook with warning react-hooks/exhaustive-deps 在 Gatsby 中启用 react-hooks/exhaustive-deps - Enable react-hooks/exhaustive-deps in Gatsby 带有自定义 IntersectionObserver 钩子的 react-hooks/exhaustive-deps 警告 - react-hooks/exhaustive-deps warning with custom IntersectionObserver hook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM