React报错:Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.

Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
    at renderWithHooks (react-dom.development.js:16317:1)
    at mountIndeterminateComponent (react-dom.development.js:20074:1)
    at beginWork (react-dom.development.js:21587:1)
    at HTMLUnknownElement.callCallback (react-dom.development.js:4164:1)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:4213:1)
    at invokeGuardedCallback (react-dom.development.js:4277:1)
    at beginWork$1 (react-dom.development.js:27451:1)
    at performUnitOfWork (react-dom.development.js:26560:1)
    at workLoopSync (react-dom.development.js:26466:1)
    at renderRootSync (react-dom.development.js:26434:1)

  解决办法:

        不能再控件的初始化中setData()。不然会导致陷入循环。

要采用这种模式:

const [data,setData] = useState([{name:'12313'}]);
 
或者用useEffect也行
posted @ 2024-03-14 14:29  信铁寒胜  阅读(51)  评论(0编辑  收藏  举报