Question

What is the useEffect dependency array for?

Vault Verified
Curated Intelligence
Definitive Source
Answer

The dependency array tells React which values the effect uses, so it can decide whether to run it again after a render. Omitting the array runs the effect after every render. Passing an empty array runs it once after mount. Listing values runs it whenever any of them changes.

The rule is that everything from component scope used inside the effect belongs in the array. The linting rule enforcing this is worth leaving enabled, because a missing dependency means the effect closes over a stale value and quietly uses outdated data.

Two failure modes dominate.

An infinite loop happens when the effect updates state that is itself a dependency, or depends on a value recreated every render. Objects, arrays and functions defined in the component body are new instances each render, so comparing them by identity always finds them changed. Moving the definition outside the component, memoising it, or depending on the specific primitive fields instead all break the cycle.

Running too often is the milder version of the same thing, usually because a dependency changes more frequently than expected.

The instinct to silence the linter is the wrong move. A missing dependency does not remove the dependency, it hides it, and the resulting bug is a value that is subtly wrong rather than an error. If the effect should genuinely not re-run when something changes, the honest fixes are moving that value into a ref, or using the functional setter form so the effect no longer needs to read the current state.

It is also worth asking whether the effect is needed at all. Deriving a value during render, rather than storing it in state via an effect, is simpler and avoids the question entirely.

Related Questions