React問題匯總


1. 調用已經寫在的組件的setState 方法,導致錯誤

問題:  warning.js:45 Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the TreeSelect component.

NewImage

解決方案: 

在 組件卸載的生命周期里面, 把在組件外部定義的變量 設置為 null, 這樣JS的垃圾回收機制就會把 組件外部定義的變量 回收. 而不造成 內存泄露

 componentWillUnmount(){ 
treeNodeClickHandler = null
},

NewImage

注意: 如果在 getinitialState 里面注冊的事件, 需要在 componentWillUnmount 里面清除掉, 否則容易造成內存泄露


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM