Here's a more in depth read here, https://github.com/facebook/create-react-app/issues/6880#issuecomment-485912528. Long story short, it comes to developer to decide what dependencies should be added to dependency array and what not, based on what functionality you're expecting, tools like eslint
won't be able to 100% accurately show errors in your code, however it can get to your attention parts of code, that maybe you should inspect more carefully. To answer your own question, ask yourself when you want your React.useEffect
to be called, what dependencies can change, and do you need your React.useEffect
to be called on those changes. Not adding correct correct dependencies to dependency array might lead to bugs where hook wouldn't be called when needed, but also adding all dependencies used in React.useEffect
to dependency array might lead to bugs where hook is called when it shouldn't be called.
In this particular scenario, asks yourself when does initial state need to be loaded:
- only on component mount? leave dependency array empty;
- when actions or dispatch changes? add required actions to dependency array.