React Reference ยท Components and Special Elements
The StrictMode component
Reference for React StrictMode: what extra checks it runs in development, why components render twice, how effects are re-run, and how to fix the bugs it finds.
StrictMode is a development-only wrapper that makes React more demanding about the code inside it. It adds no UI and has no effect in production builds. Its job is to expose bugs while you are still writing the component: impure rendering, effects that forget to clean up, and refs that are not released.
It works by doing extra work on purpose. Inside <StrictMode>, React renders each component twice, mounts each component and then immediately runs its effects' setup, cleanup and setup again, and calls ref callbacks with the same stress test. If your code is correct, the extra runs change nothing you can see. If something is wrong, the duplicate becomes visible.
import { StrictMode } from 'react'; import { createRoot } from 'react-dom/client'; createRoot(document.getElementById('root')).render( <StrictMode> <App /> </StrictMode> );
Try it
The first example logs render twice, then connect, disconnect, connect in that order: React pretends the component was unmounted and mounted again. The second one looks like it should show 1, 2, 3, but it shows 2, 4, 6: every component rendered twice, the shared counter was bumped each time, and the numbers from the second pass reached the screen. The fix is to compute the number from props or state, or to avoid reading mutable outer variables during render.
Syntax
<StrictMode>{children}</StrictMode>
| Name | Description |
|---|---|
children |
The component tree to check. Can be the whole app or one part of it. |
Returns: its children, unchanged.
There are no props to turn individual checks on or off. Placing it around the whole tree is the norm; wrapping only a section checks only that section.
What it checks
- Double render. Component functions,
useState/useMemo/useReducerinitializers and updater functions, and reducers are called twice per render. React uses one result and discards the other. The second pass in React 19 reuses the results ofuseMemoanduseCallbackfrom the first, so memoization behaves like it will in production. - Double effects. After the first mount, React runs every effect's cleanup and then its setup once more. This only happens on the initial mount, not on later updates.
- Double ref callbacks. A callback ref is called with
nulland then with the node again after mount. - Deprecated APIs. Legacy practices such as string refs or old lifecycle methods produce warnings.
Usage notes
- Production is unaffected. Builds for production contain none of this. A bug that appears only in development with StrictMode is a real bug that was merely made visible.
- Fix, don't silence. If an effect breaks under the extra cycle (for example it sends an analytics event or opens two connections), add a proper cleanup. Removing StrictMode only hides the problem.
- Fetch effects. A fetch in an effect will start twice in development. Use an ignore flag, an abort controller, or a data library that deduplicates requests. The extra request in development is not a production bug.
- Console logs appear twice. In many setups the second render's logs are dimmed or hidden by browser tools; in this editor you see both.
- Don't count renders. Never rely on a component rendering a specific number of times.
- Third-party code. Libraries that are not idempotent can misbehave. Report it upstream rather than disabling the mode for your whole app.
- Partial adoption. In a large codebase, wrap one route or feature at a time.
Related
- useEffect, useEffect cleanup, Profiler
- Lessons: Pure components, Effect cleanup