日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

React警告:Can‘t perform a React state update on an unmounted component.解決

作者:A plain girl 更新時間: 2022-07-22 編程語言

React警告:Can’t perform a React state update on an unmounted component.解決

Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.
    in Banner

在這里插入圖片描述

原因分析:

React在生命周期中設置了沒有清除的定時器,或者沒有操作了state,可能會造成數據的內存泄露問題,所以會有警告存在

例如:

   componentDidMount(){
	this.state.timer = setTimeout(()=>{
			this.next()
		},2000);
	}

解決方案:

在React的最后一個生命周期中,將函數return或者將定時器清除等。

例如:

componentWillUnmount(){
	clearTimeout(this.state.timer)
}

原文鏈接:https://blog.csdn.net/qq_43932341/article/details/125686790

欄目分類
最近更新