#react16-waning:Can't perform a React state update on an unmounted component. This is a no-op, bu...

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

这个警告的原因是当路由已经切换,过期的页面中还存在设置state的现象存在,造成内存泄漏,比较常见的就是存在定时器的情况,比如:

...
let this.timerStatus = setInterval (()=>{
  if (this.state.status === '0') {
      // 当满足某个状态就停止轮询
      clearInterval(this.timerStatus );
  } else {
      this.setState({
          status: this.state.status ++
      });
  }
},2000);

...

解决方法也简单:

...
componentWillUnmount () {
    clearInterval(this.timerStatus );
}
...
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容