![](/img/trans.png)
[英]Why am I getting the following React Native warning: Can't perform a React state update on an unmounted component
[英]Building a react app but getting an error message that I can't locate: Warning: Can't perform a React state update on an unmounted component
當您嘗試在不再安裝的組件上更新 state 時,通常會發生這種情況,這對於諸如計時器或請求之類的異步操作非常常見。
有些人喜歡在 componentDidMount() 方法上設置一個標志,並在componentDidMount()
componentWillUnmount()
上取消設置該標志,這個標志通常命名為this.mounted
。 有了這個,您可以在調用setState()
之前驗證組件的 state 並確保僅在組件仍然掛載時才調用它。
如果您有任何問題,請告訴我。
希望這會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.