zoukankan      html  css  js  c++  java
  • 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”
    

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

       // 组件加载完毕 启动定时器
          settimem: setTimeout(this.iTimer, 3000),
    
    
     // 定时器
      iTimer = () => {
        this.setState({
          timer: setInterval(() => {
            this.postuseinfo(sessionStorage.getItem("huihuaid"));
          }, 20000),
        });
    
    // 组件清除时清除定时器
      componentWillUnmount() {
        clearInterval(this.state.timer && this.state.timer);
        clearInterval(this.state.settimem && this.state.settimem);
      }
    

      

    苦心人,天不负
  • 相关阅读:
    HADOOP高可用机制
    HDFS详解
    HBase详解
    大数据计算
    Flume+Sqoop+Azkaban笔记
    Kafka知识总结
    Kafka集群安装部署、Kafka生产者、Kafka消费者
    Hive详解
    Spark面试相关
    HDFS常用操作命令
  • 原文地址:https://www.cnblogs.com/taxun/p/13347296.html
Copyright © 2011-2022 走看看