ReactEasy

What is Strict Mode?

<StrictMode> is a development-only tool that wraps part of your app and turns on extra checks to find bugs early. It double-invokes component functions, initializers and reducers, runs effects and ref callbacks an extra mount-unmount-mount cycle, and warns about deprecated APIs. It renders no UI and has no effect in production.

What it does

  • Double rendering: calls components, useState/useMemo initializers and reducers twice to expose impure rendering (side effects during render).
  • Double effects: mounts, unmounts and re-mounts each component once, so missing effect cleanups show up as bugs (duplicate subscriptions, leaked listeners). React 19.3 also double-invokes effects during hydration.
  • Double ref callbacks (React 19) to check that ref cleanups work.
  • Deprecated API warnings, such as legacy lifecycle methods.
  • React 19 reuses memoized results from the first render during the second, and DevTools dim the logs from the second render.

Why it is useful

These checks simulate what can happen in real apps: concurrent rendering may call components more than once, and features like <Activity> or Fast Refresh unmount and remount effects. Code that survives Strict Mode is safe for them.

Usage

Wrap the root (<StrictMode><App /></StrictMode>) in main.tsx; Vite and most templates do this by default. Next.js enables it in the App Router (reactStrictMode).

Common confusion

"My effect runs twice and fetches twice" in development is Strict Mode doing its job. Add a cleanup (abort the request, ignore stale results) rather than removing Strict Mode.

Code example

tsx
1// main.tsx
2import { StrictMode, useEffect } from "react";
3import { createRoot } from "react-dom/client";
4
5function Chat({ roomId }: { roomId: string }) {
6 useEffect(() => {
7 const socket = new WebSocket(`wss://chat.example.com/${roomId}`);
8 console.log("connect", roomId);
9 // Without this cleanup, Strict Mode reveals two open sockets in development
10 return () => {
11 console.log("disconnect", roomId);
12 socket.close();
13 };
14 }, [roomId]);
15 return <p>Room {roomId}</p>;
16}
17
18createRoot(document.getElementById("root")!).render(
19 <StrictMode>
20 <Chat roomId="general" />
21 </StrictMode>
22);
23// Dev console: connect, disconnect, connect (production: connect)

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