<React.StrictMode> is a developer tool that enables extra checks, warnings, and intentionally double-invokes effects during development to uncover hidden bugs and unsafe side effects.
Strict Mode does not render any visible UI node and operates only in development mode—it is automatically stripped out in production builds.
useEffect): Mounts, unmounts, and re-mounts components in development to verify effect cleanup functions work properly.componentWillMount.render methods, and state updater functions to highlight non-pure functions.import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App.jsx';
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
When running under <React.StrictMode>, React 18 intentionally runs useEffect cleanup and re-mount logic:
import React, { useEffect } from 'react';
export default function SocketLogger() {
useEffect(() => {
console.log('1. Connecting to WebSocket...');
return () => {
console.log('2. Disconnecting WebSocket...');
};
}, []);
return <div>WebSocket Monitor</div>;
}
// In Development Console under Strict Mode:
// Output:
// 1. Connecting to WebSocket...
// 2. Disconnecting WebSocket...
// 1. Connecting to WebSocket...
useEffect cleanup function rather than removing Strict Mode.process.env.NODE_ENV === 'development'.Write a useEffect subscription hook that properly cleans up an event listener when unmounted under Strict Mode.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With