The useEffect dependency array is the optional second argument that tells React when to re-run an effect. If you omit it, the effect runs after every render; if you pass [], it runs after the first render only; if you list values, it runs again whenever one of them changed since the last render. React compares each value with Object.is.
This guide explains the useeffect dependency array with small examples, shows what counts as a dependency, and covers the object and function pitfalls that cause extra runs. Examples use React 19.
| Form | Effect runs |
|---|---|
| No array | After every render |
[] | After the first render (and cleanup on unmount) |
[a, b] | After the first render and after any render where a or b changed |
The array is not a list of things you want to trigger the effect. It is a list of every reactive value the effect reads. React uses it to decide whether the effect is out of date.
A reactive value is anything declared inside your component body: props, state, and variables or functions computed from them. Values defined outside the component, such as module constants, and ref.current are not reactive.
The effect reads query and page, so both are listed. Changing either one cancels the old request through cleanup and starts a new one. The setter setResults is stable, so React does not require it in the list.
React compares each entry using Object.is, which is like === for most values. Primitives compare by value. Objects, arrays and functions compare by reference.
That has a consequence: an object created during render is a new object every render.
Here the effect runs on every render because options is never the same reference twice. Fixes, in order of preference:
id instead.useMemo only if it must be shared elsewhere.The same applies to functions defined in the component. Either define the function inside the effect, move it outside the component if it uses no reactive values, or wrap it in useCallback.
The react-hooks/exhaustive-deps rule from eslint-plugin-react-hooks warns when the array is missing a value the effect reads. Treat the warning as correct: leaving a value out gives you a stale closure, where the effect keeps using an old value from an earlier render.
If the effect should not re-run when a value changes, do not silence the rule. Restructure the code instead, for example by reading the latest value through a ref or, in React 19.2 and later, by using useEffectEvent for logic that should always see current values without being a trigger.
With [], count stays at its initial value, so the counter sticks at 1. Use the functional form setCount((c) => c + 1), which does not read count and needs no dependency.
The effect runs after every render. That is correct for a few cases but usually causes repeated work such as extra network requests.
No. The setter from useState and the dispatch from useReducer have stable identities, so listing them is allowed but unnecessary.
You can, but it is not useful. Changing ref.current does not trigger a render, so React will not notice the change.
Because a new object is created each render and compared by reference. Depend on the primitive fields instead, or move the object inside the effect.
The dependency array lists every reactive value an effect reads, and React re-runs the effect when any of them changes by Object.is. Leave it out to run every render, use [] for mount only, and let the linter catch missing entries. Watch for object and function dependencies that change on every render.