React Strict Mode
React Strict Mode: Finding Side-Effect Bugs Early
React Strict Mode (<StrictMode>) is a development-only wrapper that exposes subtle bugs before your code ships to production. It renders zero extra DOM elements and adds no overhead to production builds. Instead, it activates extra checks across any component tree nested inside it.
What Strict Mode Checks in Development
When <StrictMode> wraps your root <App /> (or a specific feature subtree), React intentionally runs certain functions twice in development:
- Component Render Functions: Runs twice to confirm your JSX logic is pure and free from shared variable mutations.
- Setup and Cleanup in
useEffect: Mounts, runs cleanup, and re-runs setup once more so missing socket disconnects or timer clears stand out immediately. - State Initializers and Updater Functions: Calls
useState(() => ...)andsetCount(prev => ...)twice to verify purity. - Deprecated API Warnings: Flags legacy string refs,
findDOMNode, and unsafe lifecycle methods in the console.
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import App from './App.jsx';
createRoot(document.getElementById('root')).render(
<StrictMode>
<App />
</StrictMode>
);
Why Double Effects Are Helpful, Not a Bug
Suppose an effect opens a WebSocket connection or adds a window scroll listener without returning a cleanup function. When Strict Mode runs setup -> cleanup -> setup, you immediately notice duplicate listeners or double chat messages in development, rather than discovering memory leaks later when users switch routes.
Common Strict Mode Mistakes
- Removing
<StrictMode>to Silence Double API Calls: Deleting<StrictMode>hides an uncleaned effect bug instead of fixing it. UseAbortControllerinside youruseEffectcleanup to cancel stale fetches cleanly. - Assuming Production Runs Twice: Production builds execute renders and effects once as normal; double invocation happens strictly in development mode.
Frequently Asked Questions
Does Strict Mode double-render slow down my live website?
No. Strict Mode checks are completely stripped when you create a production bundle (npm run build).
Why do console.log statements print twice in gray text?
React runs your render twice in Strict Mode to check purity and dims the second log in browser developer tools so you can distinguish it.
Can I enable Strict Mode for only part of an old app?
Yes. Wrap a single new feature component in <StrictMode> while leaving legacy code unwrapped during gradual upgrades.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With