zoukankan      html  css  js  c++  java
  • React篇-报错信息:warning: Can't call setState (or forceUpdate) on an unmounted component.

    报错信息是:

    Warning: Can't call setState (or forceUpdate) 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.

     

     

     

    项目中tab切换,内容封装成来一个子组件,在跳转到别的路由页面,然后再返回该页面点击tab切换的时候报上述错误,找了很久的原因,目前得到的分析应该是,在路由跳转过来的时候子组件还未加载到,此时点击切换tab栏(这里需要setState),就会报上述的错误,解决办法如下:

     

    设置变量 let isMounted = false

     

    然后在componentDidMount周期设置:

    componentDidMount(){
    isMounted = true;
    }
      
    在componentWillUnmount周期设置:
    componentWillUnmount(){
    isMounted = false
    }

    组件render这里判断isMounted的值:
    {
                            isMounted && this.state.activeIndex === 1 && <div className="tabC01">
                                                                <FTab tabCon={'tabCon01'}/>
                                                            </div>
    }

     

     

     

     

     

     

  • 相关阅读:
    杂项
    导出查询数据(大数据量)
    设置现有字段自增
    C++ 矩形交集和并集的面积-离散化
    Python使用flask架构、跨域
    匈牙利命名法
    C++ main函数
    windows编译boost
    mfc HackerTools监控键盘按键
    mfc HackerTools远程线程注入
  • 原文地址:https://www.cnblogs.com/keleyz/p/9509634.html
Copyright © 2011-2022 走看看