Heading for the future

React报错: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,

今天在开发时报了以下错误,记录一下

我们不能在组件销毁后设置state,防止出现内存泄漏的情况

出现原因直接告诉你了,组件都被销毁了,还设置个锤子的state啊

解决方案:

利用生命周期钩子函数:componentWillUnmount

将报错的地方移入此钩子里进行处理

componentWillUnmount(){
//处理逻辑
}

posted @ 2019-03-12 11:26  一只菜鸟攻城狮啊  阅读(41437)  评论(2编辑  收藏  举报