一、原因

       这种错误一般出现在react组件已经从DOM中移除。我们在react组件中发送一些异步请求的时候,就可能会出现这样的问题。举个例子,我们在componentWillMount中发送异步请求,当请求成功返回数据,我们调用setState改变组件的状态。但是当请求到达之前,我们更换了页面或者移除了组件,就会报这个错误。这是因为虽然组件已经被移除,但是请求还在执行,所以会报setState() on anunmounted component的错误。

二、解决思路

    思路很简单,我们只要在react组件被移除之前终止setState操作就可以了。

三、样例解决

    (1)定时器,在WillUnmount的时候把定期关闭。
     
   (2)处理ajax的时候,这里以jquery为例
     
    (3)低版本的react和ES5环境下
        利用isMounted(),不建议用isMounted,isMounted在通话前检查setState确实消除了警告,但也违反了警告的目的,因为现在你永远不会得到警告(即使你应该!)而且ES6上isMounted已经被禁止使用。
           
   (4)ES6环境下,推荐使用
           
  (5)fetch请求的处理
        为了让Promise可以被取消,我们处理的思路是这样的,我们在我们的Promise外面再包裹一层Promise来保证我们的Promise可以被取消。   
         
        现在我们就可以用makeCancelable来取消我们的fetch请求了。
         

React-报错Warning:setState(...)on anunmounted component的更多相关文章

  1. react报错this.setState is not a function

    当报错这个的时候就要看函数是否在行内绑定this,或者在constructor中绑定this. 我这里犯的错误的是虽然我在constructor中绑定了this,但是语法写的不正确. 错误示范: co ...

  2. react 报错的堆栈处理

    react报错 Warning: You cannot PUSH the same path using hash history 在Link上使用replace 原文地址https://reactt ...

  3. (TODO:)下载图片,报错:warning: could not load any Objective-C class information from the dyld shared cache. This will significantly reduce the quality of type information available.

    想使用NSInvocationOperation下载图片,然而并没有下载下来, NSData为nil, 还有报错:(打断点就报错) warning: could not load any Object ...

  4. ecstore在MySQL5.7下维护报错WARNING:512 @ ALTER IGNORE TABLE

    ecstore在MySQL5.7下维护报错WARNING:512 @ ALTER IGNORE TABLE 打开 /app/base/lib/application/dbtable.php , 替换A ...

  5. Docker报错 WARNING: IPv4 forwarding is disabled. Networking will not work.

    问题:创建容器的时候报错WARNING: IPv4 forwarding is disabled. Networking will not work. # docker run -it -p 3000 ...

  6. 【mysql报错】MySQL5.7.27报错“[Warning] Using a password on the command line interface can be insecure.”

    MySQL5.7.27报错“[Warning] Using a password on the command line interface can be insecure.”在命令行使用密码不安全警 ...

  7. scp执行报错WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

    linux环境执行scp时会遇到报错WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!,如下 [root@subsname home]# scp AAA. ...

  8. 关于报错:Warning: Cannot modify header information - headers already sent by (output started at

    8月5日,第一个项目即将完成,测试时,发现登录功能会出现小问题:记住密码的时候会报错 Warning: Cannot modify header information - headers alrea ...

  9. react报错 TypeError: Cannot read property 'setState' of undefined

    代码如下: class test extends Component { constructor(props) { super(props); this.state = { liked: false ...

随机推荐

  1. Linux安装git和maven的详细过程

    一.使用yum安装git 当前安装环境是centos6.5 由于在CentOS6的yum源中已经有git的版本了,可以直接使用yum源进行安装 yum -y install git 由于centos6 ...

  2. Struts(二十一):类型转换与复杂属性、集合属性配合使用

    背景: 本章节主要以复杂属性.集合属性类型转化为例,来学习这两种情况下怎么使用. 复杂对象属性转换场景: 1.新建struts_04 web.xml <?xml version="1. ...

  3. hdu1022 Train Problem I---模拟栈

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022 题目大意: 车的进出站问题,先给出N个火车,再按序列一的方式进站,判断能否以序列二的方式出站,若 ...

  4. isinstance(obj1,class) 可以判断前者是否是后者的实例

    isinstance(obj1,class) 可以判断前者是否是后者的实例

  5. Python系列之 - 锁(GIL,Lock,Rlock,Event,信号量)

    python 的解释器,有很多种,但市场占有率99.9%的都是基于c语言编写的CPython.  在这个解释器里规定了GIL. In CPython, the global interpreter l ...

  6. [LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  7. [LeetCode] Max Consecutive Ones II 最大连续1的个数之二

    Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at mos ...

  8. 机器学习技法:12 Neural Network

    Roadmap Motivation Neural Network Hypothesis Neural Network Learning Optimization and Regularization ...

  9. codevs 搜索题汇总(钻石+大师级)

    1043 方格取数 2000年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 设有N*N的方格图 ...

  10. [Codeforces 863E]Turn Off The TV

    Description Luba needs your help again! Luba has n TV sets. She knows that i-th TV set will be worki ...