React.StrictMode is a development-only feature that helps identify potential problems in a React application. It enables additional checks and warnings without affecting production builds.
Strict Mode is enabled by wrapping components with <React.StrictMode>. All child components inherit its checks automatically.
// index.js: Enabling React Strict Mode at the root level
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
In Strict Mode, React intentionally invokes certain functions twice to detect side effects. This includes rendering and effect setup/cleanup.
// ExampleComponent.js: Demonstrates double invocation in development
import React, { useEffect } from "react";
function ExampleComponent() {
useEffect(() => {
console.log("Effect runs");
return () => {
console.log("Cleanup runs");
};
}, []);
console.log("Component rendered");
return <h4>Check the console output</h4>;
}
export default ExampleComponent;
// Console output observed in development with Strict Mode enabled
Component rendered
Effect runs
Cleanup runs
Component rendered
Effect runs
This behavior occurs only in development and helps identify unintended side effects.
useEffect() correctly for side effectsconsole.count() for tracking renders<React.StrictMode>Goal: Understand how Strict Mode improves reliability by exposing hidden issues early.