Effects and synchronisation · mid
StrictMode: why effects run twice in development
In development, StrictMode renders components twice and runs every effect setup, then cleanup, then setup again, to prove your effects clean up correctly; the fix for "two API calls" is a correct cleanup, not removing StrictMode.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Development only. Production renders and runs effects once.
- What is doubled: component function calls (render), effect setup + cleanup + setup on mount, and ref callbacks on mount (React 19).
- It simulates real remounts: Fast Refresh, back/forward,
<Activity>hide/show, and future features that keep state while unmounting effects. - React 19.3: effects are also double-invoked during hydration in StrictMode, matching client-rendered roots.
- Make effects idempotent: return a cleanup that disconnects, unsubscribes, clears timers, or aborts the fetch (
AbortController). - For data fetching, a query library or Server Components remove the duplicate-request problem (dedupe + cache).
- For "run once per app load" code (analytics init), use a module-level guard or run it outside React, not a
useRefflag in every effect.
The red pen
Common traps: mistake → fix
Mistake: "Remove StrictMode to fix double API calls"
Fix: the bug is a missing cleanup; StrictMode only shows it.
Mistake: "Use a
hasRunref in every effect"Fix: it hides cleanup bugs that will appear with remounts and
<Activity>.Mistake: "StrictMode double effects happen in production"
Fix: development only.
Easy to hard
Questions interviewers ask, easy to hard
- Our effect fires twice in development and we see two API calls. A teammate wants to remove
<StrictMode>. What do you say? - What exactly does StrictMode double?
- Make this subscription effect idempotent.
- What happens in production?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.