# Strict Mode

> Why your components and effects run twice in development, and why that is actually a good thing.

Subject: React | Lesson 17 of 48 | 2 min read
Interactive lesson: https://codewithanimation.in/react/strict-mode
Previous: [Effect Cleanup](https://codewithanimation.in/react/effect-cleanup.md) | Next: [Refs & useRef](https://codewithanimation.in/react/refs-and-useref.md)

## What it is

`<React.StrictMode>` is a tool for highlighting potential problems in an application.

You usually wrap your entire app in it inside `index.js` or `main.jsx`.

Strict Mode's additional checks run only in development. Production does not receive those extra checks.

## Why does my code run twice?

Strict Mode performs extra development checks for impure rendering and missing Effect or callback-ref cleanup. These checks do not include ordinary event handlers.

With Strict Mode at the root, the initial Effect probe runs setup, cleanup, then setup again. This tests whether cleanup reverses setup; it is not a promise that the entire DOM is destroyed and recreated. Scope matters: a Strict Mode subtree does not necessarily receive the same initial Effect probe.

## Why is that a good thing?

It acts as a stress test for your code.

If you wrote a `useEffect` that adds an event listener but you *forgot* to return a cleanup function, Strict Mode will cause that event listener to be added twice. You'll notice the bug immediately in development, rather than it slowly leaking memory in production.

It helps you find impure rendering and incomplete cleanup. You still need to correct the code it exposes.

## Common mistake: The panic

Extra development requests can reveal missing cleanup or stale-result races in an Effect.

Investigate the setup and cleanup instead of disabling the check. Effects may also run again in production when dependencies change or a component mounts again; production does not mean once forever.

## Tip: Next.js and Frameworks

The Next.js App Router enables Strict Mode by default unless configured otherwise. Other frameworks and React project templates vary; inspect the application entry point and framework configuration.

## Try it: Test an Effect's cleanup

Compare lifecycle paths with real local subscriptions. The labelled model exposes missing cleanup without pretending production runs development-only checks.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/strict-mode

## Check yourself

### Question 1

Does Strict Mode double-invoke your effects in the final production build?

- A. Yes, to ensure stability.
- B. No, it is strictly a development-only tool.
- C. Only if you specifically configure it to.

Correct answer: B

The additional Strict Mode checks run only in development. Ordinary production updates or remounts can still run an Effect again.

- Why not A: It only runs in development mode.
- Why not C: React only enables its extra Strict Mode checks in development.

### Question 2

What does the extra development Effect setup/cleanup cycle help reveal?

- A. To make the application load faster.
- B. To force you to notice if you forgot to write a cleanup function.
- C. To clear the browser's cache.

Correct answer: B

An extra setup/cleanup cycle makes missing cleanup easier to observe, such as a subscription that is registered twice but never removed.

- Why not A: This check looks for correctness issues, not faster loading.
- Why not C: It has nothing to do with the browser cache.

## Remember this

- Strict Mode is a development-only wrapper.
- Its extra rendering and cleanup checks run in development; initial Effect behavior also depends on Strict Mode scope.
- It exists to expose missing cleanup functions and impure renders.
- Use extra development runs to inspect cleanup and stale work rather than assuming Effects always run once.
