Strict Mode — React tool for finding problems in your application 🔍 It doesn’t render anything visible, but helps write better code!
// Enable Strict Mode:
<React.StrictMode>
<App />
</React.StrictMode>Strict Mode in React — like a detective in a store: finds problems before they become big! 🕵️♂️
Strict Mode checks your application for potential problems:
// Wrap your app in StrictMode
<React.StrictMode>
<App />
</React.StrictMode>Important: works only in development, automatically disabled in production!
// ❌ Warning about findDOMNode
class MyComponent extends Component {
componentDidMount() {
// findDOMNode is deprecated!
findDOMNode(this).focus();
}
}// ❌ Warning about componentWillMount
class MyComponent extends Component {
componentWillMount() {
// This method is deprecated!
}
}// ❌ Bad — changing state during render
function BadComponent() {
localStorage.setItem('rendered', 'true'); // Side effect!
return <div>Component</div>;
}// Strict Mode runs effects twice
// Helps find cleanup errors!
useEffect(() => {
const subscription = subscribeToData();
// ❌ Forgot cleanup function
// Strict Mode will show the problem!
// ✅ Correct — with cleanup function
return () => {
subscription.unsubscribe();
};
}, []);// Strict Mode helps prepare for Concurrent Mode
// And other new React features// ✅ Good — enable from the start
<React.StrictMode>
<App />
</React.StrictMode>// If component causes warnings
<React.StrictMode>
<App>
<LegacyComponent /> {/* May cause warnings */}
</App>
</React.StrictMode>// ❌ Mistake — ignoring Strict Mode warnings
// "It's just warnings, no need to fix"
// ✅ Correct — fix all warnings
// Help avoid problems in the future// ❌ Mistake — thinking Strict Mode is needed in production
// It automatically disables in production!
// ✅ Correct — use only in developmentStrict Mode — like a coach who points out mistakes before you lose the match! 💪
Want more articles to prepare for interviews? Subscribe to EasyAdvice, bookmark the site and improve yourself every day 💪