react常见组件问题Can't perform a React state update on an unmounted component

Posted aloneindefeat

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. 
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状态,

  payload:values,
        callback:res=>{
          if(res.code === 0){
            notification.success({
              message: ‘密码更新成功‘,
              description: `新密码为 ${res.data}`,
              duration: 0,
            });
            successed&& successed();
              that.setState({
                updatePwdModalVisible:false
              })
            that.getCaptcha();

解决方法:利用生命周期钩子函数:componentWillUnmount,在组件卸载前清除timer

 that.pwdErrorTimer = setTimeout(() => {
              this.setState({
                updatePwdModalVisible:false
              })
            }, 1000);

  

  componentWillUnmount(){
    clearTimeout(this.pwdErrorTimer);
  }

  

以上是关于react常见组件问题Can't perform a React state update on an unmounted component的主要内容,如果未能解决你的问题,请参考以下文章

关于react-native遇到Can't find variable: TouchableHighlight

react native使用 mobx , can't find variable:Symbol

React调试错误‘Module not found: Can‘t resolve‘

[react] Module not found: Can't resolve 'schedule' in 'C:Usersadcaldvmtn7myapp (代码片段

在懒加载的Ionic工程中使用 ionic2-auto-complete 组件:Can't bind to 'dataProvider' since it isn't

找不到模块:无法解析“\node_modules\react-addons-perf”中的“react-dom/lib/ReactPerf”