功能组件渲染四次——为什么不渲染两次?

Functional component renders four times - why not twice?

我遇到了这个简单的 React 功能组件,它渲染了四次,而我希望它最初渲染一次,然后执行更新状态的 useEffect,然后再次渲染。相反,控制台发送 4 条日志输出,表明它渲染了四次。知道为什么以及关于 React 功能组件的基本生命周期的任何资源吗?

const { useState, useEffect } = React;

function App() {
  const [jobs, setJobs] = useState([]);

  useEffect(() => setJobs(["test"]), []);

  console.log("APP", jobs);

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <div>{jobs}</div>
    </div>
  );
}

ReactDOM.render(
    <React.StrictMode>
        <App/>
    </React.StrictMode>,
    document.getElementById("root")
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.12.0/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.12.0/umd/react-dom.development.js"></script>

https://codesandbox.io/s/solitary-tree-t120d?file=/src/App.js:149-191

发生这种情况是因为在您的 codesandbox 中您使用 <React.StrictMode> 来包装您的 <App />

文档明确指出 (https://reactjs.org/docs/strict-mode.html):

Strict mode can’t automatically detect side effects for you, but it can help you spot them by making them a little more deterministic. This is done by intentionally double-invoking the following functions:

Class component constructor, render, and shouldComponentUpdate methods
....