Full answer
React work splits into a render phase, where your component functions run and React works out the next UI, and a commit phase, where DOM changes are applied and effects run. React reserves the right to run the render phase more than once, to pause it, or to throw it away (for example in a transition that is interrupted). For this to be safe, rendering must be a pure calculation: the same props, state and context must always produce the same JSX, and nothing outside the component may be changed.
StrictMode enforces this in development by double invoking: the function body of components, the functions passed to useState, useMemo and useReducer, and reducers. It uses the second result for output and discards the first. If the two calls disagree, the bug shows up at once.
A typical impurity is a module-level counter or array that the component increments or pushes to during rendering. With a double render the value jumps by two. Another is mutating a prop. The fix is to compute from inputs: derive the number from props, or move the change into an event handler or an effect.
StrictMode also stress-tests effects. After the first mount React runs the effect's cleanup and then the effect again, simulating a component that is hidden and shown again, which is exactly what Activity and fast refresh do. A subscription that never unsubscribes now shows two live subscriptions. Ref callbacks get the same check in React 19.
Do not "fix" the symptom by removing StrictMode or adding a flag to skip the second effect run. That only hides the leak that real users hit when navigating back and forth. Make the cleanup complete so setup, cleanup and setup behave like one setup.
In the example, StrictMode wraps the root as in a normal app. Guest numbers are produced by an impure counter, and Seat numbers come from props. React also silences console output during the repeated calls, which is why the example records effect calls in an array.
import { StrictMode, useEffect } from 'react';
import { createRoot } from 'react-dom/client';
let guestCounter = 0;
function Guest() {
guestCounter += 1; // impure: writes outside the component while rendering
return <li>Guest #{guestCounter}</li>;
}
function Seat({ number }) {
return <li>Seat #{number}</li>; // pure: only reads props
}
const events = [];
function Listener() {
useEffect(() => {
events.push('setup');
return () => {
events.push('cleanup');
};
}, []);
return <p>Listening</p>;
}
function App() {
return (
<>
<ul>
<Guest />
<Guest />
<Seat number={1} />
<Seat number={2} />
</ul>
<Listener />
</>
);
}
const container = document.getElementById('root');
createRoot(container).render(
<StrictMode>
<App />
</StrictMode>
);
setTimeout(() => {
console.log('markup: ' + container.innerHTML);
console.log('effect calls: ' + events.join(', '));
}, 50);StrictMode wraps the root, as in a normal app. Two Guest components should be numbered 1 and 2, but the impure counter is incremented twice per render, so the numbers skip. The Seat numbers stay correct, and the effect runs setup, cleanup, setup. The calls are recorded in an array because React silences console output during the repeated calls. Output of the run: markup: <ul><li>Guest #2</li><li>Guest #4</li><li>Seat #1</li><li>Seat #2</li></ul><p>Listening</p> effect calls: setup, cleanup, setup
Key points
- The render phase must be pure: same inputs, same output, no outside writes.
- StrictMode double invokes bodies, initializers and reducers, and double mounts effects in development only.
- Module-level counters and mutated props are typical impurities. Derive values from props instead.
- Effects need complete cleanup so setup-cleanup-setup equals one setup.
- Do not remove StrictMode to silence a symptom.
Common mistakes
- Counting renders or pushing to arrays in the component body.
- Removing StrictMode because "effects run twice".
- Adding a ref flag to skip the second effect run and hiding a missing cleanup.
- Assuming the double render is a production behaviour.
Follow-ups
- Which hooks and functions does StrictMode call twice?
- How does Activity relate to the effect remount check?
- Why is a module-level cache written during render a purity problem, and how would you write a safe cache?
Tips
- Frame it as "React may repeat a render", then explain StrictMode as a test for that. This shows understanding of the model.
- Give the module counter bug as a concrete example.
- Say that cleanup must be a mirror of setup.