StrictMode is a development tool for highlighting potential problems in an application. It activates additional checks and warnings for its descendants.
StrictMode is development-only and has no effect in production builds. It doesn’t render any visible UI and doesn’t impact the user experience.Usage
Type
StrictMode is represented by the symbol Symbol.for('react.strict_mode') (the value of REACT_STRICT_MODE_TYPE in React’s source).
What StrictMode Checks
StrictMode enables several development-only behaviors to help identify issues:
1. Identifying Unsafe Lifecycles
StrictMode warns about legacy lifecycle methods that are unsafe for async rendering:componentWillMount→ UsecomponentDidMountorconstructorcomponentWillReceiveProps→ UsegetDerivedStateFromPropsorcomponentDidUpdatecomponentWillUpdate→ UsegetSnapshotBeforeUpdateorcomponentDidUpdate
2. Warning About Legacy String Refs
String refs are deprecated and will warn in StrictMode:3. Detecting Unexpected Side Effects
StrictMode double-invokes certain functions in development to help detect side effects: Functions that are double-invoked:- Function component bodies
useState,useMemo, anduseReducerinitializer functions- Class component
constructor,render, andshouldComponentUpdatemethods getDerivedStateFromPropsstatic method
4. Detecting Legacy Context API
StrictMode warns about usage of the legacy context API:5. Detecting Deprecated findDOMNode Usage
findDOMNode is deprecated and will warn in StrictMode:
6. Ensuring Reusable State (React 18+)
In React 18+, StrictMode simulates mounting, unmounting, and remounting components to ensure state can be preserved:Enabling StrictMode
For Your Entire App
Wrap your entire app in StrictMode:For Part of Your App
You can enable StrictMode for specific component trees:Nested StrictMode
StrictMode can be nested, and inner StrictMode has no additional effect:Common Issues and Fixes
Issue: My component is rendering twice!
This is intentional. StrictMode double-invokes render functions in development to help catch bugs. This only happens in development mode.Issue: My useEffect is running twice on mount!
This is intentional in React 18+. StrictMode simulates mounting, unmounting, and remounting:Issue: setState in constructor/render is causing issues
Issue: External API is being called twice
StrictMode in Tests
You can wrap your test components in StrictMode to get the same checks during testing:Migration Strategy
If you’re adding StrictMode to an existing app:- Start small: Wrap individual components or features first
- Fix warnings: Address StrictMode warnings as they appear
- Expand gradually: Move StrictMode up the tree as you fix issues
- Eventually wrap the whole app: Once all warnings are resolved
Benefits
Using StrictMode helps you:- Catch bugs early: Find issues in development before users see them
- Write better code: Encourages best practices and pure functions
- Future-proof your app: Prepares your code for future React features
- Easier refactoring: Helps identify code that will break with updates
- Better performance: Helps identify unnecessary re-renders and side effects