react 趟坑

 最近一直在做react项目,发现一个bug,困扰了我两天。

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.

稍稍翻译下:不能在已经被销毁的组件中调用setState()方案 

出现场景:跳转路由 当前组件被销毁 组件内部还存在异步操作对state的状态信息 比如http请求,定时器setTimeOut更新state

 

解决方法:

componentWillUnmount() {

  this.setState = (state,callback)=>{ return; };

}

组件被销毁之前重写setState方法 不对状态做任何改变

posted @ 2018-12-03 20:39  不系舟178  阅读(405)  评论(0编辑  收藏  举报