Why does React 18 StrictMode double-invoke your components and effects?
908 views
01
01
Understand the problem
Question presented to candidate:
"Your console log appears twice on mount and your effect fires twice. What is going on and should you fix it?"
What a strong answer should cover:
StrictMode deliberately runs things twice in development to surface two classes of bug: impure render and incomplete effect cleanup.
What is double-invoked: the component function body, useState/useReducerinitialisers and updater functions, and useMemo/useCallback factories — everything that is supposed to be pure. If running it twice changes the result, it was not pure.
Effects are treated differently: React mounts, unmounts, then mounts again — so you see effect, cleanup, effect. That tests whether your cleanup fully undoes the setup.
Why it matters beyond development: React needs to be able to discard and restart a render for concurrent features, and to remount a component with preserved state. Code that breaks under the double-invoke would break there too.
It is development-only. Production runs each once, so this is not a performance concern.
The wrong fix is a ref guard to make the effect run once. That silences the alarm and leaves the fire — the effect is not idempotent, and it will misbehave the first time the component genuinely remounts.
The right fix is a cleanup that fully reverses the setup: abort the request, unsubscribe, clear the timer.
Clarifying questions expected:
"Is this development or production?" — the answer differs entirely.
"Does the doubled run actually cause a problem?" — if yes, that is a real bug.
Code / implementation expected: Optional. An effect that breaks under the double-invoke and its fixed version is the clearest demonstration.
reactstrict-modeeffects
02
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Nudge consolestandby
Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.
03
03
Study the solution
Target Audience: Engineers preparing for React interviews — assumes effects and cleanup.
Difficulty: Hard
How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. The counts in section 3 were executed against React 19.2.8 by rendering the
Solution ready — 2 min read
Classified // press E to declassify
04
04
Explore the playground snippets
An effect that breaks under the double-invoke, and the cleanup that fixes it