react异常:Can‘t perform a React state update on an unmounted component[已解决]

Posted 勇敢*牛牛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了react异常:Can‘t perform a React state update on an unmounted component[已解决]相关的知识,希望对你有一定的参考价值。

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.

原因:

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

解决方案:

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

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

以上是关于react异常:Can‘t perform a React state update on an unmounted component[已解决]的主要内容,如果未能解决你的问题,请参考以下文章

react项目Bug:组件销毁清除监听(Can't perform a React state update on an unmounted component.)

useFetch Can't perform a React state update on an unmounted component 警告

Android异常篇 java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

React-Native首次运行提示-ReferenceError-Can-t-find-variable-fbBatchedBridge

Python连接数据库异常‘latin-1‘ codec can‘t encode characte

解决Python2.7的UnicodeEncodeError: ‘ascii’ codec can’t encode异常错误