码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-12-27 13:29:00      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:解决方法   class   stat   子函数   getc   visible   out   success   mod   

在些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

标签:解决方法   class   stat   子函数   getc   visible   out   success   mod   

原文地址:https://www.cnblogs.com/aloneindefeat/p/12106450.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!