react 使用 error 报错
在使用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 报错的更多相关文章
- 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报错解决办法 ...
- Authentication token manipulation error报错解决办法
Authentication token manipulation error报错解决办法 #参考http://blog.163.com/junwu_lb/blog/static/1916798920 ...
- 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 ...
- React Native 基础报错及解决方案记录
刚开始上手RN,碰到很多坑,记录一下.碰到问题多去看看github上面的issue! 启动命令react-native run-ios报错 1.:xcrun: error: unable to fin ...
- 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 ...
- (转)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 报错 ...
- 怎样解决Script error报错问题
如果脚本网址与网页网址不在同一个域(比如使用了 CDN), 那如果这个脚本执行报错了, 就会报:Script error. 由于同源策略, 浏览器禁止向外部脚本泄漏信息, 因此不会提供完整的报错信息, ...
- React Native 日常报错
在学习React.js 或 React Native 过程中,有时看着别人的框架或代码,但总是会出现错误,因为React或之中用到的一些包经常更新,有些代码或教程就显得过旧了. 一.日常报错 'con ...
- React Developer插件报错Cannot read properties of undefined (reading ‘forEach‘)
安装了3.6的版本React Developer 启用插件后 报错 解决 https://www.crx4chrome.com/crx/3068/ 下载 下载好后,直接拖入扩展程序中
- ios---apple mach-o linker error 报错解决
问题触发场景 在新xcode环境里配置了cocoapods,并运行了自己的项目.然后某日从其他地方clone了第三方项目,打开后,有了这个报错: 问题原因 1.用cocoapods装了第三方插件后,要 ...
随机推荐
- [FAQ] Phpstorm 代码提示功能失效问题
如果是之前有代码提示,中间突然不出现提示了,那么考虑重建一下项目索引. 示例: Refer:Phpstorm代码提示 Link:https://www.cnblogs.com/farwish/p/13 ...
- [Contract] web3.eth.getAccounts, web3.eth.getCoinbase 使用场景区别
web3.eth.getAccounts() 返回节点控制的账号列表(Promise returns Array) web3.eth.getCoinbase() 返回挖矿奖励所归集的地址(Promis ...
- vue.js写悬浮广告效果
拿上一篇运行一下,感觉自己这个效果在边界处理的更好 <template> <div class="ad"> <p>vue广告悬浮</p&g ...
- MacBook M1 虚拟机安装Windows for ARM使用体验
前言 大家好,我是 刚进入春天还没来得及踏青又开始从早忙到晚的 蛮三刀.去年给大家带来了一篇比较详尽的MacBook M1评测.评测经历了全网的热情转载,成为了我唯一的一篇爆款文章(我该哭还是该笑!) ...
- Fast Möbius Transform 学习笔记 | FMT
小 Tips:在计算机语言中 \(\cap\) = & / and, \(\cup\) = | / or First. 定义 定义长度为 \(2^n\) 的序列的 and 卷积 \(A = B ...
- 通过劫持线程arena实现任意地址分配 n1ctf2018_null
通过劫持线程arena,当堆开了一个线程之后,如果没有做好保护随之的危险也悄然而至 BUU上的n1ctf2018_null很好的说明了这个问题 题目链接:BUUCTF在线评测 (buuoj.cn) 看 ...
- 运行程序时报go: cannot find main module, but found .git/config in
编写单元测试,运行时报下面的错误 haima@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/haimait/learn/base/cheshi0 ...
- 【译】使用 GitHub Copilot 作为你的编码 GPS
GitHub Copilot 是一个改变游戏规则的人工智能助手,可以彻底改变您在 Visual Studio 中的编码流程.在我们的视频系列中,Bruno Capuano 探讨了这个智能编码伙伴如何 ...
- 2022年windows的Visual Studio常用插件及使用手册
前景提要 Viusual Studio 是一款很好用的C/C++集成开发工具,具有强大的扩展功能,好用的插件,但是,很多人都是只写了有什么插件,但是,没写怎么使用这种插件,使得使用的时候很是不方便,所 ...
- C 语言编程 — 变量与常量
目录 文章目录 目录 前文列表 变量与常量 变量 变量的类型 变量的声明 变量的定义 变量的初始化与赋值 常量 整型常量 浮点型常量 字符型场景 字符串常量 符号常量 作用域 存储类 auto 修饰符 ...