先用 npx create-react-app my-app 创建了一个 react app
然后把 ./src/App.js 改成
import React, { useState } from "react";
function App() {
const [count, setCount] = useState(0);
setCount(0);
return <div>{count}</div>;
}
export default App;
这个代码,count 的初始值是 0, setCount 的值也是 0, 按理说0===0的值是 true, 这个 setCount 不应该触发渲染才对啊, 结果发现它无限循环了, 报错:
Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
百思不得其解