简体   繁体   English

反应 | React Hook useEffect 缺少依赖项

[英]React | React Hook useEffect has a missing dependency

I want to update a value in the store ONLY ONCE AT FIRST OPENING when the page is first opened using react hook.我只想在第一次打开页面时使用 react hook 更新商店中的值。 For this, I make the second parameter of useEffect '[]' empty list.为此,我将 useEffect '[]' 的第二个参数设为空列表。 Nothing prevents it from working, but I get a warning from ESLint rules: React Hook useEffect has a missing dependency: 'changeCount'.没有什么能阻止它工作,但我从 ESLint 规则中收到警告: React Hook useEffect 缺少依赖项:'changeCount'。 Either include it or remove the dependency array react-hooks/exhaustive-deps .要么包含它,要么删除依赖数组 react-hooks/exhaustive-deps How do I remove this warning?如何删除此警告?

const App = ({UserStore:{setCount, count}}) => {
  const changeCount = () => {
    setCount();
  }

  useEffect(() => {
    changeCount();
  },[])

  return (
  ..
  )}

use this syntax to remove this eslint warning before your dependency array like this:使用此语法在您的依赖项数组之前删除此eslint警告,如下所示:

const App = ({UserStore:{setCount, count}}) => {
const changeCount = () => {
    setCount();
}

useEffect(() => {
    changeCount();
    // eslint-disable-next-line
},[])

return (
  ..
)}

changeCount is a function that is not a setState or hooks. changeCount是一个 function 不是setState或钩子。 Since you use it in useEffect React will warn you.因为你在useEffect中使用它,所以 React 会警告你。 For the reason why it warns you read this article因为它警告你阅读这篇文章的原因

Some of the answers above suggest you disable the checking, and I recommend diable only the one you are working on.上面的一些答案建议你禁用检查,我建议只禁用你正在处理的那个。 However, normally you don't need to worry about a warning.但是,通常您无需担心警告。

Move changeCount inside useEffectuseEffect移动changeCount

const App = ({UserStore:{setCount, count}}) => {
  useEffect(() => {
    const changeCount = () => {
      setCount();
    }

    changeCount();
  },[])

  return (
  ..
  )
}

Create a custom hook...创建一个自定义钩子...

export const useMountEffect = handler => useEffect(handler, []);

Consume it like像这样消费

useMountEffect(() => {
  changeCount();
});

Not only you'll get rid of this warning... but it'll be more clear that this effect will only be executed onMount ...不仅你会摆脱这个警告......而且更清楚的是,这个效果只会在onMount上执行......

TLDR TLDR

Fix eslint config "react-hooks/exhaustive-deps"修复 eslint 配置“react-hooks/exhaustive-deps”

Answer回答

It is eslint error with hook, so how about fix eslint config like这是钩子的 eslint 错误,那么修复 eslint 配置怎么样

{
  "plugins": [
    // ...
    "react-hooks"
  ],
  "rules": {
    // ...
    "react-hooks/rules-of-hooks": "error", // Checks rules of Hooks
    "react-hooks/exhaustive-deps": "off" // Checks effect dependencies
  }
}

But It can make your hook be wrong action, So the other way you can use /* eslint-disable */但它会使你的钩子成为错误的动作,所以你可以使用另一种方式/* eslint-disable */

Reference参考

01. Rules of Hooks - React Document 01. Hooks 规则 - React 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM