React.StrictMode runs extra checks in development to surface unsafe lifecycles, deprecated APIs, and side-effect bugs. It does not affect production output. StrictMode catches unsafe patterns by double-invoking in dev; ensure effects are idempotent and tested.
What is the purpose of the React.StrictMode component?
Use guided tracks for structured prep, then practice company-specific question sets when you want targeted interview coverage.
OverviewReact.StrictMode is a wrapper component provided by React to help developers identify unsafe or deprecated code patterns. It activates additional development checks and warnings to ensure components adhere to modern React practices and will behave correctly in concurrent rendering scenarios.
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
Feature | Behavior |
|---|---|
Detects Unsafe Lifecycles | Warns when using outdated lifecycle methods like |
Detects Side-Effect Issues | Runs certain functions twice (like useEffect callbacks) to ensure side effects are idempotent. |
Highlights Deprecated APIs | Warns about legacy string refs, findDOMNode, and legacy context usage. |
Identifies Unexpected Behavior | Detects potential issues in asynchronous rendering with React concurrent features. |
Why It Matters
- Encourages developers to write cleaner, future-proof React code.
- Ensures that effects and updates behave consistently in upcoming React features.
- Improves debugging and helps migrate older codebases to modern React hooks and concurrent rendering.
Important Notes
- Only active in development mode; it has no effect in production builds.
- May cause duplicate console logs or API calls in development due to intentional double-invocation of certain functions.
<li>Does not render any UI or change component behavior; it’s purely diagnostic.// Example of StrictMode highlighting a side-effect
function Example() {
useEffect(() => {
console.log('Effect triggered');
return () => console.log('Cleanup');
}, []);
return <p>Hello World</p>;
}
// The effect above runs twice in development to test cleanup behavior.
Think of React.StrictMode as React’s 'training mode' — it helps you find potential bugs early, making your app stronger and more resilient in production.
Practical scenario
In development, you notice effects running twice; StrictMode is intentionally exposing unsafe side effects.
Common pitfalls
- Assuming double-invocation happens in production (it does not).
- Writing non-idempotent effects that break when re-run.
- Doing side effects directly in render.
StrictMode makes dev noisier but catches bugs early. Test by making effects idempotent and verifying cleanup works.
- StrictMode helps detect side effects, deprecated APIs, and unsafe lifecycles.
- It runs additional checks in development without affecting production.
- Useful for ensuring your React app is ready for future updates and concurrent rendering.