一、原因

       这种错误一般出现在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. KNN算法简单应用

    这里是写给小白看的,大牛路过勿喷. 1 KNN算法简介 KNN(K-Nearest Neighbor)工作原理:存在一个样本数据集合,也称为训练样本集,并且样本集中每个数据都存在标签,即我们知道样本集 ...

  2. leetcode算法:Island Perimeter

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  3. linux系统下Apache日志分割(按天生成文件)

    Apache日志按天显示,修改Apache http.conf文件,注释默认的日志文件,修改为下面2行 ErrorLog "| /usr/local/apache/bin/rotatelog ...

  4. 三、如何使用QtDesigner

    三.如何使用QtDesigner 启动 QtDesigner,创建一个PyQt项目 拖动Label到主窗体,双击并输入自己想输入的文字 并保持为 HelloWorld.ui 此时在你Python项目下 ...

  5. Bellman-Ford算法的改进---SPFA算法

    传送门: Dijkstra Bellman-Ford SPFA Floyd 1.算法思想 Bellman-Ford算法时间复杂度比较高,在于Bellman-Ford需要递推n次,每次递推需要扫描所有的 ...

  6. ABP跨域调用API时出现的问题

    public override void Initialize() { IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAss ...

  7. html5之一些通用属性

    dir属性:定义元素内容排序方式(rtl,ltr,auto)contentEditable属性:内容是否可编辑tabindex属性:按tab键激活元素 <!DOCTYPE html>< ...

  8. CodeForces 909E Coprocessor(无脑拓扑排序)

    You are given a program you want to execute as a set of tasks organized in a dependency graph. The d ...

  9. css 相关算法

    计算 em 目标像素 除 基准像素 等于 em倍数结果: 14 / 16 = 0.875em 0.875倍也就是 14 像素 计算百分比 目标像素 除 父类总宽度 乘 一百:90 / 200 * 10 ...

  10. 一起做orb-slam(2)

    1.ushort用法? USHORT is a macro which is not part of the official C++ language (it's probably defined ...