<React.StrictMode></React.StrictMode> 组件到底做了什么?

What exactly <React.StrictMode></React.StrictMode> component does?

我在 React 实践中发现了这个组件,但我不知道确切的行为是什么。

例如

const App = () => {
  return (
    <React.StrictMode>
      <div id="cool-attribute">
        <h1>Welcome to the Jungle!</h1>
        <SearchParams />
      </div>
    </React.StrictMode>
  );
};

首先你需要了解严格模式究竟是什么。

严格模式是一种模式,在这种模式下,编译器会格外小心地处理一些额外的指令,不会自行消除静默错误,而是将它们抛出并在编码时施加更多限制。更多信息请follow

React.StrictMode 是一回事,但在专门使用 React 时提供更多限制。这里有一个综合guide

如果您期待 UI 中的内容,请停止,因为根据 React.StrictMode 文档:

StrictMode does not render any visible UI. It activates additional checks and warnings for its descendants.