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 ...
随机推荐
- Docker安装报错
这是由于overlay2不支持造成的,所以我们要关闭它. 解决办法: vim /etc/sysconfig/docker (修改文件) DNS='--selinux-enabled --log- ...
- 深入理解java:2.3.6. 并发编程concurrent包 之管理类---线程池
我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降低系统的效率,因为频繁 ...
- 跨域设置之corsheaders
安装 pip install django-cors-headers 注册应用 INSTALLED_APPS = ( ... 'corsheaders', ... ) 中间层设置 MIDDLEWARE ...
- 详解MySql的配置文件my.cnf
1.Windows下MySQL的配置文件是my.ini,一般会在安装目录的根目录. 2.Linux下MySQL的配置文件是my.cnf,一般会放在/etc/my.cnf,/etc/mysql/my.c ...
- excel常用公式--数据清洗类
trim:去除单元格两端的空格. concat/&:连接单元格内的内容. mid: 提取字符串中间的字符串. left: 提取字符串左边的字符串. right: 提取字符串右边的字符串. ...
- 第六周课程总结&试验报告(四)
一.实验目的 (1)掌握类的继承方法 (2)变量的继承和覆盖,方法的继承,重载和覆盖实现 二.实验内容 三.实验过程 1. 实验源码 package test; import java.util.Sc ...
- poj2352(树状数组)
题目链接:https://vjudge.net/problem/POJ-2352 题意:在直角坐标系中给出n个点的 (x,y),(0<=x,y<=32000),定义每个点的level为(x ...
- Solr 4.4.0安装
软件包准备 JDK http://download.oracle.com/otn/java/jdk/6u45-b06/jdk-6u45-linux-x64.bin Solr https://archi ...
- PTA(Basic Level)1011.A+B和C
给定区间 [−231,231] 内的 3 个整数 A.B 和 C,请判断 A+B 是否大于 C. 输入格式: 输入第 1 行给出正整数 T (≤10),是测试用例的个数.随后给出 T 组测试用例,每组 ...
- SQLSERVER 秘钥整理
SQLSERVER 2017 Enterprise Core 6GPYM-VHN83-PHDM2-Q9T2R-KBV83 Developer ---- Enterprise TDKQD-PKV44-P ...