I read that React.StrictMode helps enforce best practices by warning about deprecated lifecycle methods.
I’m not sure if it only targets unsafe lifecycle methods or if it can also be used with functional components.
For example:
import { StrictMode } from "react";
class Test extends Component {
render() {
return (
<StrictMode>
{/* Some child component with lifecycle methods */}
</StrictMode>
);
}
}
How effective is react.strictmode, and what exactly does it do?
Hey! I’ve wrestled with this before
. React.StrictMode is a development tool that helps you catch potential issues early, it doesn’t affect production builds.
It checks for deprecated or unsafe lifecycle methods in class components (componentWillMount, componentWillReceiveProps, componentWillUpdate).
It also works with functional components by warning about side effects or unsafe patterns in hooks.
Example:
import React, { StrictMode } from "react";
import App from "./App";
const Root = () => (
<StrictMode>
<App />
</StrictMode>
);
export default Root;
Wrapping your component tree in <StrictMode> enables these extra checks.
I always keep it enabled in development, it’s a lightweight way to enforce best practices.
I often see people think StrictMode is only for classes. It’s not!
For functional components, it helps detect side effects in useEffect hooks.
In React 18+, it intentionally double-invokes certain functions (like useEffect mount/unmount) in development to reveal side-effect bugs.
Example:
useEffect(() => {
console.log("Effect runs");
}, []);
In StrictMode, you might see this logged twice, which is normal and intentional, React wants you to notice any side-effect issues.
It doesn’t run twice in production, so don’t panic!
Besides lifecycle and hook checks, I’ve found StrictMode useful for:
-
Detecting legacy string ref usage.
-
Highlighting deprecated findDOMNode calls.
-
Warning about unexpected side effects.
I usually leave StrictMode on while developing, it’s like a developer safety net.
It’s not a silver bullet, but it helps you catch subtle bugs early.