Languages

React - how to fix "React Hook useEffect has a missing dependency" warning?

0 points
Asked by:
nickkk0
647

I am using useEffect() hook with empty array of dependencies ([]) as a second argument, and I keep getting the following warning:

React Hook useEffect has a missing dependency: 'date'. Either include it or remove the dependency array

However, the useEffect() in my code needs the empty dependency array in order to execute code on component mount.

How can I get rid of this warning?

1 answer
0 points
Answered by:
nickkk0
647

The warning you get comes from ESLint, a tool to control code quality.

You can disable ESLint for the next line using the following comment in your code:

// eslint-disable-next-line

Place the comment before the line that you want to ignore, so you won't get any warnings.

In your case that would be at the end of useEffect arrow function:

useEffect(() => {
    // useEffect function body here...

    // eslint-disable-next-line
}, []);

 

References

  1. Rules - ESLint - Pluggable JavaScript Linter
0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join