Effect lifecycle and dependencies
Effects do not have a component-style lifecycle. An Effect describes a restartable synchronization process.
Dependencies exist so React can keep an external process aligned with the current render's configuration.
Reactive valuesβ
Values declared inside a component can differ between renders:
function ChatRoom({roomId}) {
const [serverUrl, setServerUrl] = useState('https://example.com');
const options = {roomId, serverUrl};
}
roomId, serverUrl, and options are reactive. If an Effect reads a reactive value, that value normally belongs in the dependency list.
useEffect(() => {
const connection = createConnection(serverUrl, roomId);
connection.connect();
return () => connection.disconnect();
}, [serverUrl, roomId]);
Dependencies describe synchronization configurationβ
If either value changes, the old connection no longer matches the current React state.
React compares dependencies with Object.isβ
Objects and functions require extra care because identity can change even when contents appear equivalent.
Stale closuresβ
function Counter() {
const [count, setCount] = useState(0);
useEffect(() => {
const id = setInterval(() => {
console.log(count);
}, 1000);
return () => clearInterval(id);
}, []); // β count is reactive but omitted
}
React did not fail to update the closure. The code asked React to keep the synchronization process created by the first render.
Do not suppress the dependency linter casuallyβ
// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(() => {
connection.connect(roomId);
}, []);
Suppressing the rule removes a correctness check.
Move constants outside the componentβ
const serverUrl = 'https://chat.example.com';
function ChatRoom({roomId}) {
useEffect(() => {
const connection = connect(serverUrl, roomId);
return () => connection.disconnect();
}, [roomId]);
}
Moving a genuinely constant value outside proves it is not reactive.
Create objects inside the Effectβ
function ChatRoom({roomId}) {
useEffect(() => {
const options = {
serverUrl: 'https://chat.example.com',
roomId,
};
const connection = createConnection(options);
connection.connect();
return () => connection.disconnect();
}, [roomId]);
}
The dependency now expresses the real reactive configuration instead of a newly created object identity.
Functions can cause the same identity problemβ
useEffect(() => {
const request = {query, limit: 20};
fetchResults(request);
}, [query]);
Move supporting logic inside the Effect when possible. Simplify before reaching for useCallback.
Separate reactive and non-reactive Effect logicβ
React 19.2's useEffectEvent provides a clearer model for latest-value Effect logic.
Each Effect should model one synchronization processβ
useEffect(() => {
const connection = connect(roomId);
return () => connection.disconnect();
}, [roomId]);
useEffect(() => {
const subscription = subscribeToPresence(userId);
return () => subscription.unsubscribe();
}, [userId]);
roomId changes.userId changes.Dependency changes and cleanup orderβ
If roomId changes from general to travel:
The cleanup closes over values from the render that created it, so the general cleanup knows which process to stop.
Empty and missing dependency arraysβ
An empty array is correct when the Effect reads no reactive component values:
useEffect(() => {
const observer = observeWindowSize();
return () => observer.disconnect();
}, []);
No dependency array means synchronization may run after every render:
useEffect(() => {
synchronizeSomething();
});
That is relatively uncommon when an external process has explicit configuration.
State updater functions can remove unnecessary readsβ
useEffect(() => {
const id = setInterval(() => {
setCount(current => current + 1);
}, 1000);
return () => clearInterval(id);
}, []);
The updater describes a transition from previous state instead of reading the current render's count.
Debugging dependency loopsβ
Ask whether the state update is necessary, whether the value is derived data, whether an object/function is recreated every render, and whether the Effect synchronizes an external system at all.
Production example: subscription architectureβ
function StockTicker({symbol}) {
const [price, setPrice] = useState(null);
useEffect(() => {
const subscription = market.subscribe(symbol, nextPrice => {
setPrice(nextPrice);
});
return () => subscription.unsubscribe();
}, [symbol]);
return <output>{price ?? 'Loadingβ¦'}</output>;
}
Exerciseβ
Refactor a notification subscription so it reconnects only when userId changes but still uses the latest theme for toasts. Do not disable the linter.
Interview questionsβ
Junior: What is a reactive value?
Mid-level: Why can object/function dependencies cause repeated Effects?
Senior: How do you distinguish a genuine dependency from non-reactive Effect logic without suppressing exhaustive-deps?
Summaryβ
If a dependency feels wrong, redesign the Effect before overriding the linter.
Referencesβ
- https://react.dev/learn/lifecycle-of-reactive-effects
- https://react.dev/learn/removing-effect-dependencies
- https://react.dev/reference/react/useEffect
Nextβ
Continue with useEffectEvent.