React 错误处理(componentDidCatch)
前言
看react 文档突然发现有这个 错误处理函数,好像是17年9月出的,这个真的绝了可以帮助我们捕捉错误咯
React 16 将提供一个内置函数 componentDidCatch,如果 render() 函数抛出错误,则会触发该函数。
官网例子
下面这个:
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
componentDidCatch(error, info) {
// Display fallback UI
this.setState({ hasError: true });
// You can also log the error to an error reporting service
logErrorToMyService(error, info);
}
render() {
if (this.state.hasError) {
// You can render any custom fallback UI
return <h1>Something went wrong.</h1>;
}
return this.props.children;
}
}
当然你可以把这个组件封装下成为
<ErrorBoundary>
<MyWidget />
</ErrorBoundary>
然后在顶部或任何地方,你可以这样使用它
另一个特性:
componentDidCatch 是包含错误堆栈的 info 对象!
{this.state.info && this.state.info.componentStack}
当然我是这么用的在路由那边
class App extends React.Component {
constructor(props) {
super(props)
this.state = {
hasError: false
}
}
componentDidCatch(error, info) {
console.log(error, info)
this.setState({
hasError: true
})
}
render() {
return this.state.hasError ?
<h2>页面出错了404</h2>
: (
<React.Fragment>
{/* 检验是否有登录信息 */}
<AutoRoute />
{/* 有了switch后,匹配到path后就不会再匹配下去了 */}
<Switch>
<Route path="/login" component={Login}></Route>
<Route path='/register' component={Register}></Route>
<Route path='/chat/:user' component={Chat}></Route>
</Switch>
</React.Fragment>
)
}
}
其实还是组件化比较好一点,毕竟复用性比较高
React 错误处理(componentDidCatch)的更多相关文章
- React 错误边界组件
这是React16的内容,并不是最新的技术,但是用很少被讨论,直到通过文档发现其实也是很有用的一部分内容,还是总结一下- React中的未捕获的 JS 错误会导致整个应用的崩溃,和整个组件树的卸载.从 ...
- React错误总结解决方案(二)
1.React native: Cannot add a child that doesn't have a YogaNode or parent node 该错误一般是因为render方法中注释语句 ...
- webpack react 错误整理
1.ERROR in ./src/entry.js Module build failed: SyntaxError 解决方法: 安装babel-preset-react, npm install ...
- React错误总结(三)
神坑react native之Cannot Add a child that doesn't have a YogaNode to a parent with out a measure functi ...
- react错误总结
react-native 错误总结 The development server returned response error code: 500 in react-native https://b ...
- 一个自己犯的react错误
在看<react小书>高阶组件一节的时候,看到如下代码 import React, { Component } from 'react' export default (WrappedCo ...
- React错误收集
1. Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/ ...
- react 错误处理
https://www.jianshu.com/p/61d09e488743 https://codepen.io/sgroff04/pen/dVbgJy/
- react那些事儿
一.参考链接https://reactjs.org/http://react-china.org/https://doc.react-china.org/https://hulufei.gitbook ...
随机推荐
- 如何利用Nginx的缓冲、缓存优化提升性能
使用缓冲释放后端服务器 反向代理的一个问题是代理大量用户时会增加服务器进程的性能冲击影响.在大多数情况下,可以很大程度上能通过利用Nginx的缓冲和缓存功能减轻. 当代理到另一台服务器,两个不同的连接 ...
- 自定义Spring-Boot @Enable注解
Spring-Boot中有很多Enable开头的注解,通过添加注解来开启一项功能,如 其原理是什么?如何开发自己的Enable注解? 1.原理 以@EnableScheduling为例,查看其源码,发 ...
- 【HANA系列】SAP HANA的ini文件存储路径
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA的ini文件存储 ...
- Egret入门学习日记 --- 第五篇(书中 3.5节 内容)
第五篇(书中 3.5节 内容) 今天得把昨天的问题解决了才行. 去了Q群,碰到一位大大,他给我解惑了.Thanks♪(・ω・)ノ 这是我之前按照书上写的方式写的,并没有效果. 然后大大给我解答了: 后 ...
- amoeba 实现读写分离(借鉴)
1.准备三台服务器 我的是centos7版本的 做MySQL的主从状态 可以参考 https://www.cnblogs.com/chenxiaodou/articles/11993283.html ...
- Educational Codeforces Round 64 -B(贪心)
题目链接:https://codeforces.com/contest/1156/problem/B 题意:给一段字符串,通过变换顺序使得该字符串不包含为位置上相邻且在字母表上也相邻的情况,并输出. ...
- Jackson快速入门
1.Jackson的简单用法 2.Jackson框架的高阶应用 3.Jackson不支持Java8 Date解决方法 https://www.cnblogs.com/mkxzy/p/7091381.h ...
- python根据文本生成词云图
python根据文本生成词云图 效果 代码 from wordcloud import WordCloud import codecs import jieba #import jieba.analy ...
- 条件运算符在GUN C中的特殊用法.
在阅读内核源码的时候,发现了条件表达式的奇怪用法,一时没有反应过来.下面的内容是从wiki转载而来,用作回顾和备忘. 转载链接:https://zh.wikipedia.org/wiki/%E6%9D ...
- PostgreSQL-pg_ctl
命令简介 pg_ctl 启动.关闭.重启 postgres pg_ctl start [-w] [-s] [-D datadir] [-l filename] [-o options] [-p pat ...