为什么 ESLint 在 useEffect 依赖数组上触发错误

huangapple go评论77阅读模式
英文:

Why ESLint trigger error on useEffect dependancy array

问题

我在VS Code中安装了ESLint,在我的React项目中,当我使用useEffect时,它想要进行更正。代码段如下:

const navigate = useNavigate();

useEffect(() => {
    if (isAuthenticated) {
        navigate("/");
    }
}, [isAuthenticated]);

ESLint在useEffect的依赖数组中下划线标记了isAuthenticated,并告诉我:

"React Hook useEffect has a missing dependency: 'navigate'. Either include it or remove the dependency array. eslint react-hooks/exhaustive-deps"

所以我不知道useEffect的依赖数组实际上是如何工作的,或者这是ESLint的一种误解?

P.S. 简而言之,我知道这个数组用于包含会触发useEffect的内容,当它们发生变化时。我说得对吗?

英文:

I installed ESLint into VS code and in my React project it wants to make correction when i use useEffect. The segment of the code is:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

    const navigate = useNavigate();

    useEffect(() =&gt; {
        if (isAuthenticated) {
            navigate(&quot;/&quot;);
        }
    }, [isAuthenticated]);

<!-- end snippet -->

The ESLint underline me isAuthenticated inside of the dependancy array of useEffect and tells me that:

React Hook useEffect has a missing dependency: &#39;navigate&#39;. Either include it or remove the dependency array.eslintreact-hooks/exhaustive-deps

So i dont know how actualy dependancy array of useEffect work or this is some sort of missunderstaning of eslint ?

P.S. Long story short that i know about this array is there is staying thinks that trigger this useEffect when they are changed. Am i right?

答案1

得分: 0

回答你的问题,你是对的。将那个变量放在 useEffect 的末尾意味着,当依赖变量的值发生变化时,效果会运行。

ESLint 提供警告是因为它检测到 navigate 函数在 useEffect 回调内部使用,但未在依赖数组中列出。

在依赖数组中包括 navigate 函数:

useEffect(() => {
    if (isAuthenticated) {
        navigate("/");
    }
}, [isAuthenticated, navigate]); // 将 'navigate' 添加到依赖数组中

或者,你可以禁用特定行的 ESLint 规则:

// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(() => {
    if (isAuthenticated) {
        navigate("/");
    }
}, [isAuthenticated]);

如果你仍然遇到问题,请告诉我。

英文:

To answer your question, you're right. Putting that variable at the end of useEffect means that, the effect runs when dependency variable value changes.

ESLint is providing a warning because it detects that the navigate function is used inside the useEffect callback, but it's not listed as a dependency in the dependency array.

Include the navigate function from the dependency array:

useEffect(() =&gt; {
    if (isAuthenticated) {
        navigate(&quot;/&quot;);
    }
}, [isAuthenticated, navigate]); // Add &#39;navigate&#39; to the dependency array

OR, you can Disable the ESLint rule for the specific line:

// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(() =&gt; {
    if (isAuthenticated) {
        navigate(&quot;/&quot;);
    }
}, [isAuthenticated]);

Let me know, if you still having the issue.

huangapple
  • 本文由 发表于 2023年6月22日 19:44:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定