ReactMedium

What is Strict Mode double render?

In development, <StrictMode> makes React call your component functions twice per render, and also double-invokes useState, useMemo and useReducer initializers, updater functions and reducers. It also runs an extra effect and ref cleanup-and-setup cycle on mount. The goal is to reveal impure rendering and missing cleanups. Production builds render once.

What gets double-invoked

  • Component bodies (function components and class render, constructor).
  • State initializers, updater functions and reducers.
  • useMemo callbacks.
  • Effects and ref callbacks: setup, cleanup, setup on mount. React 19.3 also does this for effects during hydration.

What it catches

  • Impure render: mutating props, pushing to an outer array, generating random values used for keys, incrementing counters during render.
  • Missing cleanup: two subscriptions, two intervals, duplicated listeners, sockets left open.
  • Non-idempotent effects: effects that break when run twice, such as one-time analytics without guards.

Details in recent versions

  • React 18 dimmed the second set of console logs; React 19 keeps logs dimmed in DevTools and reuses memoized results from the first render during the second.
  • It is only active in development.

Why it reflects reality

Concurrent rendering can call components more than once before committing, and features like <Activity> and Fast Refresh unmount and remount effects. Code that tolerates Strict Mode tolerates these.

Wrong fix

Removing <StrictMode> or adding a "has run" ref to make an effect run once. Add proper cleanup or move the logic to an event handler instead.

Code example

tsx
1import { StrictMode, useEffect, useState } from "react";
2
3const globalList: string[] = [];
4
5function Impure({ name }: { name: string }) {
6 globalList.push(name); // Strict Mode doubles this -> reveals the impurity
7 return <p>{globalList.length} items</p>;
8}
9
10function Subscription() {
11 const [online, setOnline] = useState(navigator.onLine);
12 useEffect(() => {
13 const update = () => setOnline(navigator.onLine);
14 window.addEventListener("online", update);
15 window.addEventListener("offline", update);
16 // Remove this cleanup and Strict Mode shows duplicated listeners
17 return () => {
18 window.removeEventListener("online", update);
19 window.removeEventListener("offline", update);
20 };
21 }, []);
22 return <p>{online ? "Online" : "Offline"}</p>;
23}
24
25export function App() {
26 return (
27 <StrictMode>
28 <Impure name="a" />
29 <Subscription />
30 </StrictMode>
31 );
32}

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.

Open in study app