在使用react 中报错原因总结

01

// Warning: Can't call setState on a component that is not yet mounted. This is a no-op, but it might indicate a bug in your application.Instead, assign to `this.state` directly or define a `state = {};class property with the desired state in the Router2 component

// 原因是在 constructor 中 调用了 this.setState()
// 解决办法 可以将 this.setState() 操作放在 componentDidMount 中执行
componentDidMount() {
this.init()
}
init = () => {
this.setState({...})
}

02

// Warning: Can't perform a React state update 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. at Router2 (http://localhost:3000/static/js/main.chunk.js:1351:5)

// 原因是 react 在组件挂在之后进行了异步操作,在切换路由时,组件已经被卸载,此时异步操作中 callback 还在执行,因此 setState 没有得到值
// 解决办法
// 在卸载时对所有异步操作进行清除, 或者设置flag 不在进行 this.setState() 操作。
componentWillUnmount() {
this.setState = (state, callback) => {
return;
}
// this.setState = () => false;
// clearTimeout(timer)
// ajax.abort()
//
} // 在组件已经卸载时return,不去设置state:使用react组件this里面的updater属性上的isMounted方法判断组件是否存在,如果不存在,就return,不再去设置setState
if (this.updater.isMounted(this)) {
this.setState({
dashBoardDate: otherDashBoardDate
})
} else {
return
}
// react Hooks 解决:
useEffect(() => {
let isUnmounted = false
const abortController = new window.AbortController
request.get('xxxxxx').then((resp:any):any => {
if(resp.code == 200 && !isUnmounted){       this.setState({         //....       })    }
}).finally(() => {
})
return () => {
isUnmounted = true
abortController.abort
};
},[]);

react 使用 error 报错的更多相关文章

  1. eclipse里error报错Target runtime com.genuitec.runtime.generic.jee60 is not defined.

    eclipse里error报错Target runtime com.genuitec.runtime.generic.jee60 is not defined. eclipse里error报错解决办法 ...

  2. Authentication token manipulation error报错解决办法

    Authentication token manipulation error报错解决办法 #参考http://blog.163.com/junwu_lb/blog/static/1916798920 ...

  3. React Natived打包报错java.io.IOException: Could not delete path '...\android\support\v7'解决

    问题详情 React Native打包apk时在第二次编译时候报错: java.io.IOException: Could not delete path 'D:\mycode\reactnative ...

  4. React Native 基础报错及解决方案记录

    刚开始上手RN,碰到很多坑,记录一下.碰到问题多去看看github上面的issue! 启动命令react-native run-ios报错 1.:xcrun: error: unable to fin ...

  5. react 使用 ref 报错 ,[eslint] Using string literals in ref attributes is deprecated. (react/no-string-refs)

    react 项目中给指定元素加事件,使用到 react 的 ref 属性,Eslink 报错 [eslint] Using string literals in ref attributes is d ...

  6. (转)react 使用 ref 报错 ,[eslint] Using string literals in ref attributes is deprecated. (react/no-string-refs)

    原文地址:https://www.cnblogs.com/gangerdai/p/7396226.html react 项目中给指定元素加事件,使用到 react 的 ref 属性,Eslink 报错 ...

  7. 怎样解决Script error报错问题

    如果脚本网址与网页网址不在同一个域(比如使用了 CDN), 那如果这个脚本执行报错了, 就会报:Script error. 由于同源策略, 浏览器禁止向外部脚本泄漏信息, 因此不会提供完整的报错信息, ...

  8. React Native 日常报错

    在学习React.js 或 React Native 过程中,有时看着别人的框架或代码,但总是会出现错误,因为React或之中用到的一些包经常更新,有些代码或教程就显得过旧了. 一.日常报错 'con ...

  9. React Developer插件报错Cannot read properties of undefined (reading ‘forEach‘)

    安装了3.6的版本React Developer 启用插件后 报错 解决 https://www.crx4chrome.com/crx/3068/ 下载 下载好后,直接拖入扩展程序中

  10. ios---apple mach-o linker error 报错解决

    问题触发场景 在新xcode环境里配置了cocoapods,并运行了自己的项目.然后某日从其他地方clone了第三方项目,打开后,有了这个报错: 问题原因 1.用cocoapods装了第三方插件后,要 ...

随机推荐

  1. 一文详解Redis中BigKey、HotKey的发现与处理

    简介: 在Redis的使用过程中,我们经常会遇到BigKey(下文将其称为"大key")及HotKey(下文将其称为"热key").大Key与热Key如果未能及 ...

  2. 基于MaxCompute+PAI的用户增长方案实践

    ​简介: 如何通过PAI+MaxCompute完成用户增长模型AARRR全链路,包含拉新.促活.留存.创收.分享. 本文作者 李博 阿里云智能 高级产品专家 在过去一年阿里云PAI机器学习团队做了很多 ...

  3. [FE] 被动检测 iframe 加载 src 成功失败的一种思路和方式 (Vue)

    思路:设置定时器一个,n 秒后设置 404 或其它,此时给 iframe 的 onload 事件设置回调函数,加载完成则取消定时器. 示例: data () { return { handler: n ...

  4. 兼容ie8问题

    <!-- 让IE8/9支持媒体查询,从而兼容栅格 --><!--[if lt IE 9]><script src="https://cdn.staticfile ...

  5. docker容器资源配额

    1.docker 容器控制CPU docker通过cgroup来控制容器使用的资源限制,可以对docker限制的资源包括cpu.内存.磁盘 1.1 指定docker容器可以使用的cpu份额 # 查看配 ...

  6. css的animate做一个信号动画

    html <div class="jump flex-fs fadeAndScaleIn"> <span></span> <span> ...

  7. 从大数据平台CDP的架构看大数据的发展趋势

    CDP(Cloudera Data Platform)是Cloudera 和 HortonWorks 合并后推出的新一代大数据平台 ,并正在逐步停止对原有的大数据平台 CDH 和 HDP 的维护.笔记 ...

  8. oracle RDBMS Kernel Executable 占用内存过高

    oracle RDBMS Kernel Executable 占用内存过高 参考:https://www.cnblogs.com/markkang/archive/2019/11/25/1192540 ...

  9. RVM Ruby 版本管理器的删除 Gatling

    参考: https://www.jianshu.com/p/aef65d0c03a4

  10. tomcat(3)- tomcat部署zrlog

    目录 1. Tomcat单独部署 2. nginx+tomcat部署 1. Tomcat单独部署 部署场景为: 客户端:192.168.20.1 tomcat:主机名:tomcat01,地址:192. ...