Hint
Intentionally double-invokes render, effects to expose impure code and side effects — dev only, no production cost
React.StrictMode is a development tool that helps you find bugs by intentionally stressing your components.
What it does (dev only):
// StrictMode reveals this bug:
function BadComponent() {
const items = []; // 💥 rendered twice, items reset each time — detected!
items.push('item'); // pure render must not have side effects
return {items};
}
// StrictMode reveals missing cleanup:
useEffect(() => {
const ws = new WebSocket(url); // mount #1 — created
// StrictMode: unmount → cleanup should close ws
// mount #2 — second connection opened
// If no cleanup, you'd have two connections — StrictMode exposes this
return () => ws.close(); // ✅ proper cleanup
}, [url]);
// Usage
root.render(
);