DEV Community

Discussion on: React Hooks Series: useEffect

Collapse
 
spic profile image
Sascha Picard

Hey James,

thanks for your articles, I love how you take time to explain things!

I am wondering about one thing:
Did you have a particular reason to add the "setters" for the state variables created with setState (I am talking about setStart() and setCounter() ) to the dependencies arrays of both useEffect() calls?

useEffect(() => {
    if (counter === 0) {
      setStart(false)
    }
}, [
    counter, // I get it: we want to run the effect when counter changes   
    setStart  // I dont get it: this value will never change throughout the lifetime of the component
])
Collapse
 
jamesncox profile image
James Cox

This is a great question, and I want to do some more digging into this and promise to reply again with some more resources that better explain, but the short answer for now is: the linter asks me to put in those dependencies.

I guess you could argue that you don’t have to. But according to the docs “it’s totally free”. And for me I don’t like having linter warnings.

I will see if I can dig up any other better explanations and resources. Maybe someone reading this will have a better answer too. Thanks for asking and I will get back to you soon!