useEffect and synchronizing with external systems
useEffect is one of the most misunderstood React Hooks.
The most important mental model is:
An Effect is not βrun arbitrary code after renderβ.
Hook code should not be designed by mechanically translating class lifecycle methods.
Keep something outside React synchronized with the rendered component's current configuration.
Examples of external systems include:
- browser APIs;
- network connections;
- third-party widgets;
- subscriptions;
- timers;
- analytics systems;
- media APIs;
- non-React DOM libraries.
import {useEffect} from 'react';
function ChatRoom({roomId}) {
useEffect(() => {
const connection = createConnection(roomId);
connection.connect();
return () => {
connection.disconnect();
};
}, [roomId]);
return <h1>Room: {roomId}</h1>;
}
The component describes UI. The Effect keeps an external connection synchronized with roomId.
Rendering, events, and Effects are differentβ
A React component contains three different kinds of logic.
Pure calculations from the current props, state, and Context.
Runs because a specific user action such as click, submit, or input happened.
Runs because the rendered component must keep an external system aligned with its current configuration.
Render logicβ
const total = items.reduce((sum, item) => sum + item.price, 0);
This is a calculation. It belongs in render.
Event logicβ
function handlePurchase() {
submitOrder(cart);
}
The POST happens because the user clicked Buy. That belongs in the event handler.
Effect logicβ
useEffect(() => {
const connection = connectToRoom(roomId);
return () => connection.disconnect();
}, [roomId]);
The connection should exist whenever this rendered room exists. That is synchronization.
Basic syntaxβ
useEffect(() => {
// start synchronization
return () => {
// stop synchronization
};
}, [dependencies]);
The dependency list is not a scheduling wish list. It describes the reactive values your synchronization process uses.
Effect lifecycleβ
Think in terms of start and stop, not mount/update/unmount.
This model scales better than trying to translate class lifecycle methods into Hook code.
Cleanupβ
Cleanup should undo whatever the Effect set up.
Subscription:
useEffect(() => {
function handleOnline() {
setOnline(true);
}
window.addEventListener('online', handleOnline);
return () => {
window.removeEventListener('online', handleOnline);
};
}, []);
Timer:
useEffect(() => {
const id = setInterval(refresh, 30_000);
return () => {
clearInterval(id);
};
}, [refresh]);
Connection:
useEffect(() => {
const connection = createConnection(serverUrl, roomId);
connection.connect();
return () => connection.disconnect();
}, [serverUrl, roomId]);
A good question is:
If this Effect ran, what must happen before it runs again or disappears?
Dependency arraysβ
No dependency arrayβ
useEffect(() => {
synchronize();
});
React considers re-synchronization after every commit.
Empty dependency arrayβ
useEffect(() => {
connectOnceForThisMountedInstance();
return disconnect;
}, []);
This says the Effect does not read changing reactive values from the component.
It should not be used to imitate a class lifecycle method by habit.
Reactive dependenciesβ
useEffect(() => {
const connection = createConnection(roomId);
connection.connect();
return () => connection.disconnect();
}, [roomId]);
When roomId changes according to Object.is, React re-synchronizes.
You do not choose dependencies independently from the codeβ
Bad reasoning:
useEffect(() => {
connect(roomId);
}, []); // β "I only want it once"
The Effect reads roomId. If the component can render with different rooms, the synchronization must follow the current room.
Better:
useEffect(() => {
const connection = connect(roomId);
return () => connection.disconnect();
}, [roomId]);
If you disagree with the linter, first ask whether the Effect is designed correctly.
Strict Mode and duplicate setup in developmentβ
In development Strict Mode, React deliberately performs an extra setup β cleanup β setup cycle to expose Effects that are missing cleanup.
The goal is not to make an Effect "run once despite Strict Mode".
The goal is to make this sequence safe.
Bad workaround:
const didRun = useRef(false);
useEffect(() => {
if (didRun.current) return;
didRun.current = true;
connection.connect();
}, []);
This may hide the development signal while still failing to disconnect when the component actually leaves the screen.
Correct approach:
useEffect(() => {
const connection = createConnection();
connection.connect();
return () => connection.disconnect();
}, []);
Fetching in an Effectβ
A client-only app may sometimes fetch inside an Effect:
useEffect(() => {
const controller = new AbortController();
async function load() {
const response = await fetch(`/api/products?q=${query}`, {
signal: controller.signal,
});
const data = await response.json();
setProducts(data);
}
load().catch(error => {
if (error.name !== 'AbortError') {
setError(error);
}
});
return () => controller.abort();
}, [query]);
But raw Effect-based data fetching creates architectural concerns:
- caching;
- deduplication;
- waterfalls;
- race conditions;
- loading/error coordination;
- preloading;
- server rendering.
Later chapters will compare raw Effects with framework data APIs and server-state libraries.
Race conditionsβ
Consider two requests:
Starts earlier, but the network response is slower.
Starts later and finishes first, so its result is the newer intended UI.
Without cancellation or stale-result protection, the older React request can overwrite the newer Redux result.
Without protection, stale request A might overwrite the newer Redux result.
Cancellation with AbortController, framework loaders, or a data library can prevent this class of bug.
Common infinite loopβ
useEffect(() => {
setFiltered(products.filter(matchesQuery));
}, [products, filtered]); // β
The Effect updates filtered, which is also a dependency, creating another synchronization cycle.
More importantly, filtered is derived data and likely does not need an Effect at all:
const filtered = products.filter(matchesQuery);
Object and function dependenciesβ
This creates a new object every render:
const options = {roomId, serverUrl};
useEffect(() => {
connect(options);
}, [options]);
Because the object identity changes, the Effect may reconnect unnecessarily.
Often the cleanest solution is to construct the object inside the Effect:
useEffect(() => {
const options = {roomId, serverUrl};
const connection = connect(options);
return () => connection.disconnect();
}, [roomId, serverUrl]);
Do not add useMemo automatically. First simplify the synchronization boundary.
Effects should represent separate synchronization processesβ
Bad:
useEffect(() => {
connectToRoom(roomId);
logVisit(userId);
subscribeToNotifications();
}, [roomId, userId]);
These processes have different reasons to re-synchronize.
Prefer separate Effects when they synchronize independent systems:
useEffect(() => {
const connection = connectToRoom(roomId);
return () => connection.disconnect();
}, [roomId]);
useEffect(() => {
logVisit(userId);
}, [userId]);
Debugging an Effectβ
Use this sequence:
If you cannot name the external system, the Effect deserves extra scrutiny.
When should I use an Effect?β
Use one when rendering the component requires synchronization with something React does not control.
Examples:
- WebSocket connection;
- browser event listener;
- media playback API;
- map/chart widget;
- timer;
- third-party DOM library;
- certain client-side network synchronization cases.
When should I NOT use an Effect?β
Usually not for:
- deriving values from props/state;
- sorting/filtering for render;
- responding to a click;
- resetting one state variable because another changed;
- notifying a parent about state that the parent could own directly;
- chaining internal state transitions.
Those cases are covered in You Might Not Need an Effect.
Exerciseβ
Create a ChatRoom component with:
roomIdprop;- simulated
connect()anddisconnect()methods; - correct cleanup;
- a room selector in the parent.
Log every connect/disconnect and explain why switching rooms produces:
Then test under Strict Mode.
Interview questionsβ
Junior: What problem does useEffect solve?
Mid-level: Why are Effect dependencies determined by reactive values rather than by how often you want the Effect to run?
Senior: How would you decide whether a data-fetching Effect should remain local code or move to framework/server-state infrastructure?
Summaryβ
Remember:
Referencesβ
- https://react.dev/learn/synchronizing-with-effects
- https://react.dev/learn/lifecycle-of-reactive-effects
- https://react.dev/reference/react/useEffect
Nextβ
Continue with You Might Not Need an Effect.