我想要 count varible 來計算我的應用程式中違反了多少規范。所以我normsCount: 0在 state 中宣告了變數。在功能檢查規范如下和增量計數。
const {
normsCount
} = this.state
if (isViolationPass_Out_HMR) {
this.setState({
normsCount: normsCount 1
})
}
但收到此錯誤
ExceptionsManager.js:180 Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.
This error is located at:
in NavigationContainer (at App.js:412)
in App (at renderApplication.js:47)
in RCTView (at View.js:34)
in View (at AppContainer.js:107)
in RCTView (at View.js:34)
in View (at AppContainer.js:134)
in AppContainer (at renderApplication.js:40)
任何幫助提前致謝。
uj5u.com熱心網友回復:
如果你在你的渲染函式中設定了一個狀態,狀態將會改變,這會導致重新渲染。這導致了無限回圈。
您需要確保僅在更改時觸發一次增加。
uj5u.com熱心網友回復:
您在渲染函式內部呼叫的函式內部呼叫 setState ,該函式不斷呼叫 setState 導致無限回圈。要阻止這種情況,您必須對任何事件(如 onclick)執行 setState。檢查在渲染函式中呼叫的 setState 并在事件上執行該 setState。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/357551.html
