Strict 模式
一句话解释
Strict 模式是 development-仅 wrapper 组件那个 intentionally 双-invokes 确定函数到 surface 隐藏 bugs.
用大白话说
<React.StrictMode> is a wrapper that enables extra checks and warnings in development. It does nothing in production — it is stripped out. Its purpose is to help you find problems early.
In Strict Mode, React intentionally double-invokes: component render functions, the useState initializer, the useMemo and useReducer initializers, and useEffect setup/cleanup. This surfaces bugs in code that assumes functions run exactly once — impure renders, effects that don't clean up properly, etc.
Strict Mode also warns about deprecated APIs, unsafe lifecycles, and legacy ref APIs. You should wrap your entire app in Strict Mode during development. The double-invocation is safe if your components are pure and your effects have proper cleanup — which they should be anyway.
示例
import { StrictMode } from "react";
function App() {
return (
<StrictMode>
<Root />
</StrictMode>
);
}Wrapping app in StrictMode 启用 extra development 检查. 它有 否副作用 in production 构建.
相关术语
相关资源
相关课程
- React 最佳实践
学习 Strict 模式 and development 最佳实践.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。