Can’t call setState (or forceUpdate) on an unmounted component 警告处理方法
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 警告处理方法的更多相关文章
- 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 ...
- Cant't call setState(or forceUpdate) on an unmount component. 报错的可能性原因
react 小白编程 遇到了如下错误 调试了很久没找到到底为啥 后来发现,是因为多次将组件挂在到根节点的原因导致的 使用路由之后,只需要使用 ReactDOM.render()方式将最外层的路由挂在到 ...
- 关于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 ...
- RN 各种小问题
问题:vs code 无法启动 双击无反应 方法:控制台 输入 netsh winsock reset 重启 vs 问题:RN Debugger Network 不显示 源网页:https://git ...
- react一些问题
一.死循环 1.问题描述 function handleClick() { this.setState({count: ++this.state.count}); console.log(" ...
- [技术博客]React-Native中的组件加载、卸载与setState问题
React-Native中的组件加载.卸载与setState问题. Warning: Can only update a mounted or mounting component. This usu ...
- RN开发中的报错以及告警
报错一: Attempted to transition from state `RESPONDER_INACTIVE_PRESS_IN` to `RESPONDER_ACTIVE_LONG_PRES ...
- React源码解析:setState
先来几个例子热热身: ......... constructor(props){ super(props); this.state = { index: 0 } } componentDidMount ...
- React的setState分析
前端框架层出不穷,不过万变不离其宗,就是从MVC过渡到MVVM.从数据映射到DOM,angular中用的是watcher对象,vue是观察者模式,react就是state了. React通过管理状态实 ...
随机推荐
- OO第三次课程总结分析
OO第三次课程总结分析 规格化设计发展历史 在网上找了好久也没找到合适的信息,稍稍参考了同学的博客.大致如下:最初的的软件并没有形式化方法,随着软件工程的兴起,为了便于工程间的协调管理,人们提出采用工 ...
- Огонек--灯光--IPA--俄语
苏联老歌总让人沉浸其中.
- rnn-手写数字识别-网络结构-shape
手写数字识别经典案例,目标是: 1. 掌握tf编写RNN的方法 2. 剖析RNN网络结构 tensorflow编程 #coding:utf-8 import tensorflow as tf from ...
- 牛客第三场多校 H Diff-prime Pairs
链接:https://www.nowcoder.com/acm/contest/141/H来源:牛客网 Eddy has solved lots of problem involving calcul ...
- vim 使用和配置
vim 启动的时候,默认加载用户目录下.vimrc的配置文件,如果不存在,则会加载系统配置文件/etc/vim/vimrc ~/.vimrc文件配置 #避免中文乱码set fileencodings= ...
- js面向对象插件的做法框架new goBuy('.cakeItem',{ add:'.add', reduce:'.reduce' },[1,0.7,0.6]);
/*弹窗购买蛋糕*/;(function(){ var $DialogBg=$(".Dialogbg-Select"); var $Dialog=$(".Dialog-S ...
- 20165326 java第三周学习笔记
纸质学习笔记 代码托管
- 在使用Idea配置jQuery的问题
今天使用idea中引入jQuery代码时,发生的几个错误,时刻提醒 1.jQuery的驱动包要放置在web目录下 2.引入jQuery的驱动包时,语句格式为<script></scr ...
- php优秀框架codeigniter学习系列——constants.php
该文件位于application/config/constants.php.
- 微软Power BI 每月功能更新系列——3月Power BI 新功能学习
本月对Power BI Desktop 来讲是非常令人兴奋的一个月!!!以下是3月份最新的功能,下面马上带领大家看看本月的更新内容. 报表功能 报告页面工具提示(预览) 通过报告页面工具提示功能,可 ...