← Back to Chapters

React StrictMode

⚛️ React StrictMode

? Quick Overview

React.StrictMode is a development-only helper that highlights potential problems in React applications. It performs additional checks, warnings, and validations without affecting production builds.

? Key Concepts

  • Runs only in development mode
  • Does not render visible UI
  • Helps detect unsafe patterns early
  • Prepares apps for future React features

⚛️ Introduction

React.StrictMode is a tool that helps developers identify potential problems in their React applications. It runs only in development mode and does not affect production builds.

Think of it as React’s “lint mode” — it performs additional checks and warnings to help write cleaner, future-proof code.

? How to Use StrictMode

? View Code Example
// index.js - enabling StrictMode at root level
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);

You can also wrap specific components to isolate checks in smaller parts of the component tree.

? What StrictMode Checks

  • Detects usage of unsafe or deprecated lifecycle methods.
  • Warns about legacy string refs and improper ref usage.
  • Identifies unexpected side effects during rendering.
  • Ensures predictable behavior in concurrent rendering.
  • Double-invokes lifecycle methods to verify idempotence.

⚙️ Example: Detecting Side Effects

? View Code Example
// Component demonstrating double execution in StrictMode
import React, { useEffect } from "react";

function Example() {
useEffect(() => {
console.log("Effect executed");
}, []);

console.log("Component rendered");
return <p>Check your console!</p>;
}

export default Example;

When wrapped in StrictMode, the console will log:

? View Console Output
// StrictMode intentional double invocation
Component rendered
Effect executed
Component rendered
Effect executed

This helps detect unwanted side effects such as API calls during rendering.

? Interactive Simulator

Use this simulator to visualize how StrictMode forces your effects to run twice (mount -> unmount -> mount) to catch cleanup bugs.

// Visual Console Output...

? StrictMode in React 18+

  • Simulates concurrent rendering behavior
  • Runs effects multiple times
  • Validates transitions and Suspense boundaries

⚠️ Common Misunderstandings

  • “StrictMode causes bugs.” — It only exposes existing ones.
  • “It slows my app.” — Only in development.
  • “I should disable it.” — Fix warnings instead.

? Tips & Best Practices

  • Keep StrictMode enabled during development
  • Review console warnings frequently
  • Avoid side effects inside render logic
  • Use React DevTools for render analysis

? Try It Yourself

  1. Enable StrictMode in your app
  2. Add a console log inside useEffect
  3. Observe double execution
  4. Refactor to idempotent logic

Goal: Understand how React.StrictMode improves code safety and future compatibility.