前端数据大部分来源于后端,需要向后端发起异步请求,而在使用reactjs的时候,如果这个组件最初加载的时候就发起这个异步请求,然后在返回结果中进行setState({}),这时候有可能会遇到这个警告:

Warning:setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component.This is a no-op.
Please check the code for the xxx component.

  

通常是因为组件并没有装载上便开始执行setState({}),这时候,我们可以在组件中写入:

componentWillMount(){
this.mounted = true;
this.getData();
} componentWillUnmount() {
this.mounted = false;
}

 

然后在异步请求返回结果后setState的时候进行判断:

if(this.mounted){
this.setState({
});
}

这个警告便消除了~

使用reactjs遇到Warning: setState(...): Can only update a mounted or mounting component.的更多相关文章

  1. 关于Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op.的解决方案

    Warning: setState(...): Can only update a mounted or mounting component. This usually means you call ...

  2. React + antd 组件离开页面以后出现Can only update a mounted or mounting component 的解决办法

    做项目的过程中,来回切换页面时,一直遇到Can only update a mounted or mounting component 这个问题,原因是当离开页面以后,组件已经被卸载,执行setSta ...

  3. ReactJS的开发日常

    在用React框架开发的日子里,踩的坑真不少!今天就来说说这个关于组件的周期,说的可能不是很清楚,但是也给自己留下一个踩坑的纪念,如有不妥 还望大家指点一二 Warning: setState(... ...

  4. 在React组件unmounted之后setState的报错处理

    最近在做项目的时候遇到一个问题,在 react 组件 unmounted 之后 setState 会报错.我们先来看个例子, 重现一下问题: class Welcome extends Compone ...

  5. 在React 组件中使用Echarts

    在完成一个需求的时候碰到一个场景需要使用柱状图.涉及到可视化,第一反应当然是Echarts了.平时用js加载Echarts组件很方便,但是在React中就要费下神了.各种连蒙带猜实现了.edmo里的E ...

  6. react项目开发中遇到的问题

    前言 作为一个前端爱好者来说,都想在react上一试生手,那么在搭建react项目开发时,肯定会有这样或者那样的问题,尤其是对初学者来说,下面就个人在开发过程中遇到的问题总结一下,好在有google帮 ...

  7. react相关小技巧

    一.我们在项目中切换路由的时候可能会遇到 Warning: setState(...): Can only update a mounted or mounting component. This u ...

  8. React源码解析:setState

    先来几个例子热热身: ......... constructor(props){ super(props); this.state = { index: 0 } } componentDidMount ...

  9. [技术博客]React-Native中的组件加载、卸载与setState问题

    React-Native中的组件加载.卸载与setState问题. Warning: Can only update a mounted or mounting component. This usu ...

随机推荐

  1. C++ test的使用

    http://www.parasoft.com/jsp/trial_request.jsp?itemId=303 去下载,原来是个商业的测试软件,还要去购买,这个成本太大了.. http://down ...

  2. JDBC的介绍

    JDBC详解   1.JDBC是什么? JDBC(JAVA DataBase Connection)即JAVA数据库连接技术,JDBC API是一个Java API,可以访问任何类型表列数据,特别是存 ...

  3. Maven学习之(四)Maven插件创建web项目

    另一种maven web项目的创建. 创建出来的目录是这样的,此时试一下,不能加入到tomcat中去启动. 这里要将项目转化为web项目. 右键->项目 选中下面的动态web项目,然后OK 此时 ...

  4. java 支付宝wap支付初识

    最近突然想弄下支付宝的支付,因为感觉很好玩.中间遇到很多问题,查查找找,总算是整了两天给整出来了,这里为自己记录下. 第一步:直接去安卓支付宝的官方文档去,写的很清楚了已经,这里有源码https:// ...

  5. dubbo2.5.3注解版

    1.环境      在机器192.168.0.4机器上安装了zookeeper,用于dubbo的服务注册,安装教程在另外一篇博客 http://www.cnblogs.com/520playboy/p ...

  6. EFM32 DMA/PRS例程

    /**************************************************************************//**  * @file  * @brief H ...

  7. Jackson2.1.4 序列化对象时,过滤null的属性 empty的属性 default的属性

    在进行序列化如何过滤为null的属性,empty的属性,或者default的属性. 一.全局注册 objectMapper.setSerializationInclusion(Include.ALWA ...

  8. poj3208 Apocalypse Someday 数位dp+二分 求第K(K <= 5*107)个有连续3个6的数。

    /** 题目:poj3208 Apocalypse Someday 链接:http://poj.org/problem?id=3208 题意:求第K(K <= 5*107)个有连续3个6的数. ...

  9. Node.js连接postgres

    一.下载Node.js postgres驱动 Node.js里面没有postgres模块的,我们需要安装node-postgres模块. node-postgres模块的下载地址为:https://g ...

  10. MATLAB中常用的排列、组合、阶乘函数

    1.求n的阶乘,方法如下:a.factorial(n)b.gamma(n+1)c.v='n!'; vpa(v) 2.求组合(数),方法如下:a.combntns(x,m)    列举出从n个元素中取出 ...