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)
}
648

被折叠的 条评论
为什么被折叠?



