简体   繁体   English

如何修复无限重新渲染?

[英]How to fix infinite re-render?

Error: Maximum update depth exceeded.错误:超出最大更新深度。 This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate.当组件在 componentWillUpdate 或 componentDidUpdate 中重复调用 setState 时,就会发生这种情况。 React limits the number of nested updates to prevent infinite loops. React 限制嵌套更新的数量以防止无限循环。

Found the answers to my question, but, nevertheless, I can't understand how to fix this?找到了我的问题的答案,但是,我不明白如何解决这个问题?

 const { useState } = React, { render } = ReactDOM function App() { const [visible, setVisible] = useState(false); const [visible2, setVisible2] = useState(false); const arr1 = { company: [ { id: "random1", companyName: "Apple" }, { id: "random2", companyName: "Samsung" } ] }; const onDataHandle = () => { return arr1.company.map(items => { return ( <div> <span key={items.id}> {items.companyName} <span onClick={onHandleVisible}>Details</span> </span> <br /> </div> ); }); }; const onHandleVisible = () => { setVisible(!visible); }; const onHandleVisible2 = () => { setVisible2(!visible2); }; return ( <div className="App"> <button onClick={onHandleVisible2}>Show</button> {visible && onDataHandle()} </div> ); } render ( <App />, document.getElementById('root') )
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.12.0/umd/react.production.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.11.0/umd/react-dom.production.min.js"></script><div id="root"></div>

I understand that this is due to the endless re-rendering, but what are the solutions?我知道这是由于无休止的重新渲染,但解决方案是什么?

You have multiple problems in your code logic (that is not entirely clear, I must say):您的代码逻辑中有多个问题(我必须说,这并不完全清楚):

  • Show button onClick triggeres onHandleVisible2 callback which sets visible2 state variable to true , but there's nothing in your code depending on that state variable, so nothing happens显示按钮onClick触发onHandleVisible2回调,该回调将visible2状态变量设置为true ,但您的代码中没有任何取决于该状态变量的内容,因此没有任何反应
  • The block {visible && onDataHandle()} is supposed to trigger onDataHandle() (which never happens for above reason - visible stays equal to false ), but onDataHandle() (even though attempts to return some JSX) will not add anything to render within <App /> as it is not a ReactJS component{visible && onDataHandle()}应该触发onDataHandle() (由于上述原因从未发生过 - visible保持等于false ),但onDataHandle() (即使尝试返回一些 JSX)不会添加任何渲染在<App />因为它不是 ReactJS 组件
  • (the minor one) if your intention with onDatahandle() was to return some component, wrapping up your span with extra <div> doesn't make much sense. (次要的)如果您使用onDatahandle()意图是返回某个组件,那么用额外的<div>结束您的跨度并没有多大意义。

With all the above issues fixed, you would get something, like:解决了上述所有问题后,您会得到一些东西,例如:

 const { useState } = React, { render } = ReactDOM function App() { const [visible, setVisible] = useState(false); const [visible2, setVisible2] = useState(false); const arr1 = { company: [ { id: "random1", companyName: "Apple" }, { id: "random2", companyName: "Samsung" } ] }; const Data = () => ( <div> { arr1.company.map(items => ( <span key={items.id}> {items.companyName} <span onClick={onHandleVisible}>Details</span> {visible && <span>There go {items.companyName} details</span>} <br /> </span>)) } </div> ) const onHandleVisible = () => { setVisible(!visible); }; const onHandleVisible2 = () => { setVisible2(!visible2); }; return ( <div className="App"> <button onClick={onHandleVisible2}>Show</button> {visible2 && <Data />} </div> ); } render ( <App />, document.getElementById('root') )
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.12.0/umd/react.production.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.11.0/umd/react-dom.production.min.js"></script><div id="root"></div>

Note one important issue stays unsolved in above code block: you used single variable ( visible ) for your entire app, so if you decide to control visibility of details for each item independently, you won't be able to do that with your current approach.请注意,上面的代码块中一个重要问题仍未解决:您对整个应用程序使用了单个变量 ( visible ),因此如果您决定独立控制每个项目的details可见性,则使用当前方法将无法做到这一点. But that's a totally different issue which you may post as a separate question.但这是一个完全不同的问题,您可以将其作为单独的问题发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM