Skip to main content

Strict Mode

<StrictMode> enables extra development-only checks that help surface React bugs early.

A common application root looks like:

import {StrictMode} from 'react';
import {createRoot} from 'react-dom/client';
import App from './App.jsx';

createRoot(document.getElementById('root')).render(
<StrictMode>
<App />
</StrictMode>,
);

Why does Strict Mode exist?โ€‹

React wants components to be safe under rendering patterns where work can be repeated, interrupted, cleaned up, or restarted.

Strict Mode deliberately stresses assumptions that often hide bugs in ordinary development.

Mental model:

Strict Mode makes hidden assumptions visibleThe development checks expose bugs that repeated or restartable React work would otherwise reveal later.
Component appears correct
The happy path may hide impurity or incomplete cleanup.
React stresses the assumptions
Repeated render, setup/cleanup cycles, and ref callback checks make unsafe behavior reproducible.
Underlying issue is identified
Impure render ยท missing cleanup ยท unsafe ref logic ยท misplaced side effect.
Component becomes restart-safe
The same code is easier to reason about in production rendering architectures.

Strict Mode does not run these checks to make development annoying. It makes problems reproducible before production architecture makes them harder to diagnose.

Development vs productionโ€‹

Strict Mode checks are development-only.

Strict Mode development checks vs production
DevelopmentStrict Mode stress checks

Extra render checks ยท Effect setup/cleanup checks ยท ref callback checks ยท deprecation warnings.

ProductionNormal production execution

Development-only Strict Mode stress checks are removed from the production build.

Do not remove Strict Mode merely because development logs or requests reveal duplicated behavior. First ask whether the code has correct cleanup and purity.

Re-rendering to find impure render logicโ€‹

React components should be pure calculations of UI from props, state, and Context.

Bad:

function StoryTray({stories}) {
stories.push({id: 'create', label: 'Create Story'});

return stories.map((story) => (
<Story key={story.id} story={story} />
));
}

The component mutates a prop array. If the render is repeated, another item is pushed again.

Better:

function StoryTray({stories}) {
const nextStories = [
...stories,
{id: 'create', label: 'Create Story'},
];

return nextStories.map((story) => (
<Story key={story.id} story={story} />
));
}

The repeated render exposes the mutation bug; it does not create the bug.

Effects and cleanupโ€‹

An Effect synchronizes with an external system.

useEffect(() => {
const connection = createConnection(roomId);
connection.connect();

return () => {
connection.disconnect();
};
}, [roomId]);

During development, Strict Mode can run an extra setup/cleanup cycle to reveal missing cleanup.

Useful reasoning:

Strict Mode Effect stress cycle
1setup
2cleanup
3setup again

If that sequence breaks your integration, the Effect probably does not fully undo what it sets up.

Ref callbacksโ€‹

Ref callbacks can also receive extra development checks. If a ref callback registers something, its cleanup should correctly undo that registration.

This matters for cases such as:

  • maintaining your own node collection;
  • connecting observers;
  • integrating imperative widgets.

"My Effect runs twice"โ€‹

Do not start with:

How do I force this to run once?

Start with:

Why is repeated setup revealing a problem?

Common causes include:

  • no cleanup;
  • cleanup does not reverse setup;
  • an Effect is performing event logic that belongs in a click/submit handler;
  • an Effect is being used to derive state that could be calculated during render;
  • a request needs cancellation or deduplication in the application architecture.

The later Effects chapter will cover these cases in depth.

Should I disable Strict Mode?โ€‹

Usually, no.

Disabling the checks hides symptoms without improving the component model.

There can be migration situations involving old third-party integrations where Strict Mode exposes compatibility problems. Treat that as technical debt to understand, not as evidence that Strict Mode itself is incorrect.

React 19 noteโ€‹

React 19 changed some Strict Mode development behavior. For example, memoized results from useMemo and useCallback are reused during the second development render, and ref callback checks were strengthened.

The enduring rule is more important than memorising the exact stress-test implementation:

Write components so render is pure and synchronization has complete cleanup.

Debugging checklistโ€‹

When Strict Mode reveals duplicate-looking behavior:

  1. identify whether it happens during render, an Effect, or a ref callback;
  2. check whether render mutates props, state objects, module values, or external systems;
  3. check Effect cleanup symmetry;
  4. move user-event work into event handlers when appropriate;
  5. remove unnecessary Effects;
  6. confirm the behavior in a production build only after fixing the development warning.

Exerciseโ€‹

Find the bug:

function ChatRoom({roomId}) {
useEffect(() => {
const connection = createConnection(roomId);
connection.connect();
}, [roomId]);

return <h1>Room {roomId}</h1>;
}

Questions:

  • What external resource is created?
  • What should cleanup do?
  • Why is a repeated development setup useful here?

Interview questionsโ€‹

Juniorโ€‹

Does Strict Mode's extra development behavior also happen in the production build?

Mid-levelโ€‹

Why can Strict Mode reveal a missing Effect cleanup?

Seniorโ€‹

How are purity and cleanup related to React's ability to restart or discard rendering work safely?

Referencesโ€‹

Nextโ€‹

Continue with JSX.