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.

不能在已经被销毁的组件中调用setState()方案
出现场景:跳转路由 当前组件被销毁 组件内部还存在异步操作对state的状态信息 比如http请求,定时器setTimeOut更新state

思路就是在组件卸载之前 控制异步不进行setState
解决方案一:组件被销毁之前重写setState方法 不对状态做任何改变
componentWillUnmount(){
this.setState = (state,callback)=>{
return;
};
}
解决方案二:组件被销毁之前 可以setState 销毁之后就不能setState
componentWillMount() {
this._isMounted = true
fetch('网络请求').then (status, response)=>{
if (this._isMounted) {
this.setState({
activityTipsImg: response.data.tips_url,
activityTipsContent: response.data.tips_content
})
}
});
}
componentWillUnmount() {
this._isMounted = false
}
---------------------
作者:SalahMan
来源:CSDN
原文:https://blog.csdn.net/softwarenb/article/details/81123389
版权声明:本文为博主原创文章,转载请附上博文链接!

Can’t call setState (or forceUpdate) on an unmounted component 警告处理方法的更多相关文章

  1. 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 ...

  2. Cant't call setState(or forceUpdate) on an unmount component. 报错的可能性原因

    react 小白编程 遇到了如下错误 调试了很久没找到到底为啥 后来发现,是因为多次将组件挂在到根节点的原因导致的 使用路由之后,只需要使用 ReactDOM.render()方式将最外层的路由挂在到 ...

  3. 关于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 ...

  4. RN 各种小问题

    问题:vs code 无法启动 双击无反应 方法:控制台 输入 netsh winsock reset 重启 vs 问题:RN Debugger Network 不显示 源网页:https://git ...

  5. react一些问题

    一.死循环 1.问题描述 function handleClick() { this.setState({count: ++this.state.count}); console.log(" ...

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

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

  7. RN开发中的报错以及告警

    报错一: Attempted to transition from state `RESPONDER_INACTIVE_PRESS_IN` to `RESPONDER_ACTIVE_LONG_PRES ...

  8. React源码解析:setState

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

  9. React的setState分析

    前端框架层出不穷,不过万变不离其宗,就是从MVC过渡到MVVM.从数据映射到DOM,angular中用的是watcher对象,vue是观察者模式,react就是state了. React通过管理状态实 ...

随机推荐

  1. day22-python操作mysql2

    数据库连接池 python编程中可以使用MySQLdb进行数据库的连接及诸如查询/插入/更新等操作,但是每次连接mysql数据库请求时,都是独立的去请求访问,相当浪费资源,而且访问数量达到一定数量时, ...

  2. awk计算最大值,最小值,平均值的脚本

    传入至少三个数字参数到脚本awk_file,并计算出最大,最小,平均值.需要判断传入的数字是否足够,否则输出警告信息.平均值保留两位小数. 如执行bash awk_file 3 4 6 5,脚本输出结 ...

  3. windows上dubbo-admin的安装

    dubbo-admin的安装 (1)先下载好zookeeper包,因为启动dubbo-admin时需要先启动zookeeper zookeeper:dubbo的注册中心(自己下载 ,找到bin目录下的 ...

  4. mysql 给表和字段加注释

    给表加注释: ALTER TABLE table_name COMMENT='这是表的注释'; 给列加注释: ALTER table table_name MODIFY `column_name` d ...

  5. FPGA中IBERT核的应用(转)

    https://wenku.baidu.com/view/50a12d8b9ec3d5bbfd0a74f7.html (必看)    摘要 IBERT即集成式比特误码率测试仪,是Xilinx专门用于具 ...

  6. Day12作业及默写

    1.整理今天的博客,写课上代码,整理流程图. 2.用列表推导式做下列小题 li=['alex','wusir','abds','meet','ab'] a. 过滤掉长度小于3的字符串列表,并将剩下的转 ...

  7. Hibernate乐观锁无法Catch到org.hibernate.StaleObjectStateException

    Hibernate乐观锁无法Catch到org.hibernate.StaleObjectStateException时,请Catch HibernateOptimisticLockingFailur ...

  8. POJ 2369 Permutations(置换群概念题)

    Description We remind that the permutation of some final set is a one-to-one mapping of the set onto ...

  9. TEST mathjax

    这里是第一个公式 $ F = ma^2 $ \[ \text{Reinforcement Learning} \doteq \pi_* \\ \quad \updownarrow \\ \pi_* \ ...

  10. lua 取table长度

    http://blog.csdn.net/wangmanjie/article/details/52793902 static int unbound_search (Table *t, unsign ...