在使用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. iOS 端容器之 WKWebView 那些事

    ​简介: 本文主要是关于在端容器设计开发过程中,WKWebView 使用上遇到的一些问题和解决办法​ 作者 | 驽良 来源 | 阿里技术公众号 一  背景 熟悉 iOS\macOS Hybrid 混合 ...

  2. 用python编写向通信产品发送AT指令的程序实例

    一.安装pyserial包pip install pyserial 二.实例代码 # -*- coding: utf-8 -*- import time import hashlib from ser ...

  3. WinDbg 设置在加载到某个 DLL 进入断点

    本文记录如何在 WinDbg 里,设置在加载到某个 DLL 时,自动进入断点.通过此方式用来定位是哪个业务模块加载了某个 DLL 模块 在 WinDbg 里面,可以附加到现有进程,也可以启动某个进程. ...

  4. SpringBoot项目预加载数据——ApplicationRunner、CommandLineRunner、InitializingBean 、@PostConstruct区别

    0.参考.业务需求 参考: https://www.cnblogs.com/java-chen-hao/p/11835120.html#_label1 https://zhuanlan.zhihu.c ...

  5. 数仓OLAP技术

    数据应用,是真正体现数仓价值的部分,包括且又不局限于 数据可视化.BI.OLAP.即席查询,实时大屏,用户画像,推荐系统,数据分析,数据挖掘,人脸识别,风控反欺诈,ABtest等等 OLAP(On-L ...

  6. Linux中的umask

    在Linux中,当创建一个文件或者目录的时候,系统会自动为这个文件或者目录赋予默认的权限,而umask命令就是用来控制这个默认权限的. 查看umask umask的查看有两种方式,一种不带选项-S,一 ...

  7. oracle中commit之后如何进行回滚

    commit之后 第一种: 记住大概的时间,获取前大概时间的数据. select * from Test as of timestamp to_timestamp('2021-12-08 09:30: ...

  8. python教程2:if...else...+循环

    一.if判断 有单分支.双分支.多分支,下面就是一个多分支的案例: 二.缩进 三.for循环 四.while循环  五.其他 random模块  string模块

  9. Python基础篇(安装)

    Python简介 Python是Guido van Rossum发布于1991年的一种计算机程序设计语言.是一种动态的.面向对象的脚本语言,是一种解释型的,弱类型的高级计算机语言.需要注意的是pyth ...

  10. linux常用关机/重启命令:shutdown,init 0,init 6

    目录 一.使用shutdown关机,重启,定时关机 二.使用init关机/重启 一.使用shutdown关机,重启,定时关机 1.设置计算机10分钟之后关机 [root@node5 ~]# shutd ...