我正在尝试新的React Hooks的useEffect
API,它似乎在无限循环中永远运行!我只希望回调useEffect
运行一次。这是我的代码供参考:
单击“运行代码片段”以查看“Run useEffect”字符串被无限打印到控制台。
function Counter() {
const [count, setCount] = React.useState(0);
React.useEffect(() => {
console.log('Run useEffect');
setCount(100);
});
return (
<div>
<p>Count: {count}</p>
</div>
);
}
ReactDOM.render(<Counter />, document.querySelector('#app'));
<script src="https://unpkg.com/react@16.7.0-alpha.0/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16.7.0-alpha.0/umd/react-dom.development.js"></script>
<div id="app"></div>