The dependency array controls when an effect re-synchronizes. After each commit, React compares every dependency to its value from the previous render using Object.is. If any changed, it runs the previous cleanup, then the effect again. No array means the effect runs after every commit, [] means only on mount (cleanup on unmount), and [a, b] means on mount and whenever a or b changes. The array must list every reactive value the effect reads (props, state, and values derived from them). It's a correctness requirement, not an optimization knob.
useEffect(() => { log('every commit'); });
useEffect(() => {
const conn = connect();
return () => conn.close();
}, []); // mount / unmount
useEffect(() => {
const conn = connect(roomId);
return () => conn.close();
}, [roomId]); // re-connects when roomId changes
Think of an effect as "keep this external system synchronized with these values", not as "run on mount". When roomId changes, React disconnects from the old room (cleanup) and connects to the new one.
In development with Strict Mode, every effect also runs setup → cleanup → setup once on mount, to verify that your cleanup works.
Any value from the component scope that can change between renders: props, state, context values, and variables or functions declared in the component body. Stable values can be omitted (setters from useState, dispatch, refs' .current container, module-level constants), though including them is harmless.
function Search({ query }) {
const options = { query, limit: 10 }; // new object every render
useEffect(() => {
fetchResults(options);
}, [options]); // runs after EVERY render
}
Fixes, in order of preference:
[query].useMemo/useCallback to keep its identity stable (the React Compiler can do this automatically).Omitting a dependency to "run it only once" creates stale closures:
useEffect(() => {
const id = setInterval(() => setCount(count + 1), 1000);
return () => clearInterval(id);
}, []); // bug: count is always 0 inside
Fix it with an updater function (setCount(c => c + 1)). Or, if you need to read the latest value without re-running the effect, use useEffectEvent (stable since React 19.2) for the non-reactive part, or a ref.
ignore flag or AbortController in the cleanup.Object.is comparison, cleanup-before-rerun, and the three forms.exhaustive-deps.useEffectEvent).Spotted a mistake or a rights concern? Report an issue