Why does React Strict Mode render components and run effects twice?
BeginnerReact interview questionBy UIRound, updated 21 November 2026
Short answer
<StrictMode> deliberately does extra work in development to expose bugs early. It calls your component functions twice to catch side effects during render, and on mount it runs every effect's setup, then its cleanup, then setup again, to catch missing or incorrect cleanup. Production builds do none of this. The fix is to make renders pure and effects symmetric, not to turn Strict Mode off.
What runs twice
| What | Development behaviour under Strict Mode | The bug it exposes |
|---|---|---|
| Component body | Called twice per render | Mutating props, globals or outside variables while rendering |
useState and useReducer initializers, reducers |
Called twice | Impure initializers and reducers |
useMemo calculations |
Called twice when they recompute | Impure calculations |
useEffect and useLayoutEffect |
Extra setup, cleanup, setup cycle on mount | An effect with no cleanup, or a cleanup that does not undo the setup |
| Ref callbacks (React 19) | Extra attach, cleanup, attach on mount | Ref callbacks that never clean up |
React keeps the result of one of the two render calls and discards the other. Effects are not doubled on updates: when a dependency changes you get one cleanup and one setup, exactly as in production. Strict Mode also warns about some deprecated APIs.
Why double rendering
React assumes that rendering is pure: same props and state in, same JSX out, nothing changed outside. A pure function called twice gives the same result, so a correct component cannot tell. An impure one shows the problem immediately:
const seen = [];
function Row({ item }) {
seen.push(item.id); // side effect during render: runs twice in development
return <li>{item.name}</li>;
}The fix is to move the side effect into an event handler or effect, or compute the value from props instead of storing it outside. React relies on purity because concurrent rendering can start a render, throw it away and start again.
Why mount, unmount, remount for effects
React wants to be able to remove part of the UI and restore it with its state, for example with <Activity mode="hidden">, and Fast Refresh already re-runs effects when you edit a file. An effect that only works when it runs exactly once per lifetime breaks in those cases, and also in ordinary ones such as a user navigating away and back. Strict Mode simulates that cycle once on mount so the bug shows up on your machine.
// Broken: in development this opens two connections and never closes one
useEffect(() => {
const socket = connect(roomId);
}, [roomId]);
// Fixed: setup and cleanup mirror each other
useEffect(() => {
const socket = connect(roomId);
return () => socket.disconnect();
}, [roomId]);With the cleanup in place, the user cannot tell setup, cleanup, setup from a single setup. The detailed order of every call, with logs, is in why React effects run twice, and the general rules are in how useEffect works.
Fixing the common cases
Data fetching. Two requests in development are fine if the first result is ignored. Abort or ignore it in the cleanup:
useEffect(() => {
const controller = new AbortController();
fetch(`/api/users/${id}`, { signal: controller.signal })
.then((r) => r.json())
.then(setUser)
.catch((e) => { if (e.name !== "AbortError") setError(e); });
return () => controller.abort();
}, [id]);This also fixes the real race where id changes before the first response arrives. A data library such as TanStack Query handles it for you.
Subscriptions, timers, listeners. Return the matching unsubscribe, clearInterval or removeEventListener.
Things that must happen once per app load, such as initialising an analytics SDK, do not belong in a component effect. Run them at module level or behind a module-level flag.
Things that happen because the user did something, such as sending a purchase event or submitting a form, belong in the event handler, not in an effect.
Where Strict Mode is on
In plain React you opt in by wrapping the tree in <StrictMode>, usually around the root. Next.js turns it on by default for the App Router; the Pages Router needs reactStrictMode: true in next.config.js. You can also wrap only part of a tree to adopt it gradually.
Common follow-up questions
Does Strict Mode slow down production?
No. It is a development-only check. Production renders each component once per render and runs each effect setup once per mount.
Why do I see my console.log twice?
Because the component body runs twice. React 19 prints logs from both calls, and React DevTools can dim or hide the second one.
Should I use a ref to stop the effect running twice?
No. A useRef guard hides the symptom in development but leaves the missing cleanup in place, and the effect still breaks when the component really unmounts and remounts. Write the cleanup instead. Practise this by replacing a useIsMounted hook with an effect that aborts.
Is it safe to just remove StrictMode?
It makes the symptom disappear, not the bug. The same bug appears in production whenever a component remounts. Fix the code and keep the check.
Mistakes to avoid
Assuming the double effect run happens in production and adding workarounds for it
Writing effects without cleanup for connections, intervals and listeners
Mutating variables outside the component during render
Sending analytics or POST requests from a mount effect instead of the event that caused them
Guessing the call order instead of knowing it
Sources
Practise it in code
Interviewers rarely stop at the definition. These questions open in an editor with tests.
- Predict side effects during render under
StrictMode, Suspense and errors, then move them to effectsHard40 min - Predict the order of
useEffect,useLayoutEffectand cleanups in parent and child componentsMedium30 min - Build
useIsMounted, then replace it with an effect that aborts and ignores stale resultsMedium30 min - Predict how many times effects run when dependencies are objects, arrays and functionsMedium20 min
- Debug an infinite
useEffect()loop and prevent stale asynchronous responsesMedium35 min