- continuing to read A complete guide to
useEffect
.- the deps array is a hint for a react component to know everything the components depends on. Things go wrong when you try to hack it into the mental model of lifecycle events. Many times it has been explained, using am empty array is a way of saying ‘only run this once’ e.g ‘onMount’. This is not really correct. Always specify all dependancies!
“But I only want to run it on mount!”, you’ll say. For now, remember: if you specify deps, all values from inside your component that are used by the effect must be there. Including props, state, functions — anything in your component.
- bring your functions that you only want triggered once into the the useEffect if you want to avoid declaring it as a dependancy.
- Decoupling Updates from Actions is taking time to grok…
- the deps array is a hint for a react component to know everything the components depends on. Things go wrong when you try to hack it into the mental model of lifecycle events. Many times it has been explained, using am empty array is a way of saying ‘only run this once’ e.g ‘onMount’. This is not really correct. Always specify all dependancies!