【发布时间】:2021-10-16 02:23:31
【问题描述】:
我已将我的应用程序降至最低限度,以试图了解它为什么会无限循环......但我不明白。
const App = () => {
console.log("just logging that the app ran!")
const [data, setData] = useState('initial state');
const getAsset = async () => {
console.log("logging that getAsset ran!")
setData("new state!")
console.log(data)
}
getAsset();
return (
<div >
{data}
</div>
);
}
export default App;
任何指针?
错误信息:
react-dom.development.js:14997 Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
【问题讨论】:
-
这能回答你的问题吗? How to async await in react render function?
-
TL;DR:
setData触发一个新的渲染,它再次调用getAsset,这是循环!
标签: javascript reactjs asynchronous async-await