Loading...
Loading...
By continuing to use the platform, you accept the terms of the Privacy Policy and the use of cookies.
React.StrictMode is a tool for highlighting potential problems in an application. It activates additional checks and warnings only in development mode.
import React from 'react';
import ReactDOM from 'react-dom';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
Production:
StrictMode doesn't affect production builds. All checks work only in development.
StrictMode helps:
StrictMode intentionally calls functions twice to help find side effects:
function Counter() {
const [count, setCount] = useState(0);
console.log('Render'); // In StrictMode will output twice!
return (
<div>
<p>{count}</p>
<button onClick={() => setCount(count + 1)}>+</button>
</div>
);
}
Called twice:
useState, useMemo, useReducerrender, shouldComponentUpdate, getDerivedStateFromPropslet globalCounter = 0;
function Component() {
// Side effect outside useEffect!
globalCounter++; // In StrictMode will increment twice
return <div>Counter: {globalCounter}</div>;
}
let globalCounter = 0;
function Component() {
useEffect(() => {
// Side effects in useEffect
globalCounter++;
return () => {
globalCounter--; // Cleanup
};
}, []);
return <div>Counter: {globalCounter}</div>;
}
StrictMode will warn about using deprecated lifecycle methods:
class MyComponent extends React.Component {
componentWillMount() {
// Warning: deprecated method
}
componentWillReceiveProps(nextProps) {
// Warning: deprecated method
}
componentWillUpdate(nextProps, nextState) {
// Warning: deprecated method
}
render() {
return <div>Hello</div>;
}
}
class MyComponent extends React.Component {
static getDerivedStateFromProps(props, state) {
// Instead of componentWillReceiveProps and componentWillUpdate
return null;
}
componentDidMount() {
// Instead of componentWillMount
}
getSnapshotBeforeUpdate(prevProps, prevState) {
// To get information before DOM update
return null;
}
componentDidUpdate(prevProps, prevState, snapshot) {
// Work with snapshot
}
render() {
return <div>Hello</div>;
}
}
class Parent extends React.Component {
static childContextTypes = {
color: PropTypes.string
};
getChildContext() {
return { color: 'purple' };
}
render() {
return <Child />;
}
}
const ThemeContext = React.createContext('light');
function Parent() {
return (
<ThemeContext.Provider value="dark">
<Child />
</ThemeContext.Provider>
);
}
function Child() {
const theme = useContext(ThemeContext);
return <div>Theme: {theme}</div>;
}
You can apply StrictMode only to specific components:
function App() {
return (
<div>
<Header />
<React.StrictMode>
<Sidebar />
<Content />
</React.StrictMode>
<Footer />
</div>
);
}
Header and Footer won't be checked, only Sidebar and Content.
In React 18 StrictMode simulates unmounting and remounting components:
function Component() {
useEffect(() => {
console.log('Mount');
return () => {
console.log('Unmount');
};
}, []);
return <div>Hello</div>;
}
In StrictMode will output:
Mount
Unmount
Mount
This helps ensure the component properly cleans up resources when unmounting.
function Component() {
console.log('Render'); // Outputs twice in StrictMode
return <div>Hello</div>;
}
Remember that double call is only in development. In production there will be one call.
React DevTools automatically removes duplicate logs from StrictMode:
// Install React DevTools extension
// Duplicate logs will be hidden automatically
function Component() {
if (process.env.NODE_ENV === 'development') {
// Logs only in development, but still twice
console.log('Render');
}
return <div>Hello</div>;
}
StrictMode helps write quality code. But there are cases:
If a library causes warnings you can't fix:
function App() {
return (
<div>
<React.StrictMode>
<MyApp />
</React.StrictMode>
{/* Third-party library without StrictMode */}
<ThirdPartyComponent />
</div>
);
}
If double logs interfere with debugging, temporarily disable:
// Temporarily for debugging
// <React.StrictMode>
<App />
// </React.StrictMode>
Don't forget to restore StrictMode after debugging!
Add StrictMode when creating project:
// index.js
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>
);
Don't ignore StrictMode warnings. They point to real problems.
Make sure tests run with StrictMode:
// setupTests.js
import { render } from '@testing-library/react';
function renderWithStrictMode(ui) {
return render(
<React.StrictMode>
{ui}
</React.StrictMode>
);
}
No, in production StrictMode is completely disabled. All checks work only in development.
This is intentional StrictMode behavior to detect side effects. In production code will execute once.
It's recommended to wrap the entire application, but you can apply selectively to problematic parts.
React.StrictMode:
In interviews:
Important to be able to: