#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 );
}
...

你可能感兴趣的:(#react16-waning:Can't perform a React state update on an unmounted component. This is a no-op, bu...)