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 ...
随机推荐
- 阶段3 2.Spring_09.JdbcTemplate的基本使用_6 JdbcDaoSupport的使用以及Dao的两种编写方式
复制三个出来.分别叫做 OrderDaoImpl.ProductDaoImpl.UserDaoImpl 复制这三个出来就是为了解决重复性代码的问题. 每个dao中都有这段代码.这些都是重复性的代码.在 ...
- bootstrap datetimepicker、bootstrap datepicker日期组件对范围的简单封装
1.bootstrap datepicker 使用 <div class="row form-group"> <label class="control ...
- swagger-ui升级swagger-bootstrap-ui界面好看到起飞
如果项目已经集成了swagger,只需要在pom.xml添加,如果你的项目没有集成swagger,自行百度或看最下方的链接 swagger-bootstrap-ui是Swagger的前端UI实现,目的 ...
- 【HANA系列】SAP ECLIPSE中创建ABAP项目失败原因解析
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP ECLIPSE中创建AB ...
- 常用ARM指令
常用ARM指令1:数据处理指令 mov mvn MOV(MOVE)指令可完成从另一个寄存器.被移位的寄存器或将一个立即数加载到目的寄存器 MOV R0,R1;R1的值传到R0 MOV R3,#3 ...
- windows OS安全配置【持续更新20190618】
https://www.52stu.org/?p=76 来源:5号暗区 5号黯区 五号黯区 5号暗区 windows系统的一些加固方法等 关闭445端口: REG ADD HKLM\SYSTEM\Cu ...
- win10 hhctrl.ocx 丢失
1.我的是从同事电脑上复制过来的,他电脑也是win102.复制文件“hhctrl.ocx”到系统目录下 32位系统目录为:C:\WINNT\System32:64位系统为C:\Windows\Sys ...
- centoss7下将命令加开机服务
https://www.cnblogs.com/hxun/p/11075755.html
- Node.js 博客搭建
Node.js 博客搭建:https://www.linuxidc.com/Linux/2017-02/140115.htm https://www.cnblogs.com/mrcln/p/93087 ...
- 多个电脑上免密登陆命令、scp远程拷贝、修改文件的用户和组
多个电脑上免密登陆命令: 1.ssh-keygen 生成密钥 2.ssh-copy-id IP 拷贝公钥到指定服务器并授权 3.ssh Ip 验证登录,已无需输入密码 scp远程拷贝: ...