美文网首页
[已解决]react异常:Can't perform a Rea

[已解决]react异常:Can't perform a Rea

作者: 张培跃 | 来源:发表于2020-04-21 22:45 被阅读0次
报错如下:
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.
原因:

react组件已经被销毁,而此时我们的异步操作(通常为调用接口更改state操作)还未结束。当我们的异步执行完成后要执行setState操作时,已经无法获得组件信息,由此造成该异常!

解决方案:

我们应该在组件中通过componentWillUnmount钩子函数在组件销毁的时候将异步方法撤销:

componentWillUnmount() {
    this.setState = ()=>false;
}

—————END—————
喜欢本文的朋友们,欢迎关注公众号 张培跃,收看更多精彩内容!!!

相关文章

网友评论

      本文标题:[已解决]react异常:Can't perform a Rea

      本文链接:https://www.haomeiwen.com/subject/fqzmihtx.html