Strict Mode and Detecting Unintentional Side Effects0%

Strict Mode and Detecting Unintentional Side Effects

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Strict Mode and Detecting Unintentional Side Effects

1. What is React StrictMode?

<React.StrictMode> is a built-in development tool provided by React to highlight potential problems in an application. Like JavaScript's "use strict", it activates additional runtime checks and warnings for its child components.

StrictMode is enabled by wrapping your root component tree inside <React.StrictMode> in src/main.jsx:

JSX
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App.jsx';
 
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<App />
</React.StrictMode>
);

2. The Development-Only Guarantee

A critical point of reassurance for every React engineer is: StrictMode checks run ONLY in development mode. StrictMode has zero impact on production builds. It does not add overhead, double-invoke functions, or increase bundle size in production.

3. Why Does StrictMode Double-Invoke Functions?

If you add console.log('Rendering component') inside a component or console.log('Running effect') inside useEffect, you will notice that in development, it logs twice!

Output
Console Output:
Rendering component
Rendering component
Running effect setup
Running effect cleanup
Running effect setup

Why does React do this on purpose?

React's future roadmap and concurrent rendering capabilities rely on components being pure functions. An impure function produces subtle bugs, race conditions, and memory leaks that can be difficult to spot in simple development tests.

To expose accidental side effects, StrictMode deliberately:

  1. 1
    Re-renders component bodies twice: Ensures that component rendering logic is pure and produces the same output without mutating external variables.
  2. 2
    Re-runs effects twice (Mount -> Cleanup -> Mount): Verifies that your useEffect has a valid cleanup function that correctly tears down subscriptions, event listeners, and timers.

4. Catching an Impure Bug with StrictMode

Consider this accidental mutation:

JSX
// ❌ Impure Component
let guestList = [];
 
export default function GuestBadge({ name }) {
guestList.push(name); // Mutates an external variable during render!
return <div>Guests: {guestList.length}</div>;
}

In standard production, rendering this with name "Aman" might display Guests: 1.

Under StrictMode in development, React re-renders twice, so guestList.push runs twice! The screen displays Guests: 2!

The developer immediately notices that the count is wrong, uncovering the accidental external mutation that would have caused severe bugs in production.


Interactive Quiz

Practice Quiz

Question 1
What is the primary purpose of <React.StrictMode>?
Question 2
Does React StrictMode run in production builds?
Question 3
Why does React StrictMode intentionally run component functions and effects twice in development?
Question 4
If a console.log inside a component body prints twice in your Vite dev server, what is the cause?
Question 5
Can <React.StrictMode> be applied to only a specific section of an application?
Next Lesson

Capstone Project: Comprehensive Task and Todo Application

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext