Incorrect mental models: - Running code on mount - Running code on some renders but not all
It’s more specific than that.
Primarily, it’s for synchronizing. React handles synchronizing the rendered DOM for a component. But if there’s something else that needs to be updated depending on the state of the component, you do that in an effect. For example, the web page title, or setting up a web socket or interval.
Teardown is for when we are done with the current component props, either because it is being unmounted or because props have changed. If anything needs to be undone, you do that in the cleanup function.
But is it okay to use it for:
So what’s with the dependency array, and why does it need to be exhaustive?
A reason it’s risky to disable the exhaustive-dependencies checks:
The better way is to keep all the dependencies, and then add logic for when some code within the effect should or shouldn’t be run. That is more explicit logic, and is more robust against future changes.