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 ...
随机推荐
- 二十九:数据库之SQLAlchemy连接数据库
手写SQL复用性不高,SQL越复杂越容易出bug,而且容易引起SQL注入,所以现在Ibanez使用orm框架将数据库映射为数据模型,这是使用SQLAlchemy 准备工作,安装SQLAlchemy和P ...
- RabbitMQ问题解决
1.访问http://localhost:15672/#/,输入用户名.密码登录报错500 解决方法:在快捷程序处找到RabbitMQ Service -stop停止服务,然后再点击RabbitMQ ...
- Elasticsearch 为何要在 7.X版本中 去除type 的概念
背景说明 Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎.无论在开源还是专有领域,Lucene可以被认为是迄今为止最先进.性能最好的.功能最全的搜索引擎库. El ...
- dapper使用时性能优化
数据库中类型 Area 数据库类型 varchar dapper 来操作数据库,不能直接写 sql Area=@Area) //dapper 对C#中的字符串类型 默认是对应数据库nva ...
- GET 和 POST 区别?网上多数答案都是错的!
最近在看<HTTP权威指南>这本书,对HTTP协议有了更深一层的了解. 在我们面试过程中关于HTTP协议有两个经典的面试题: 1. 谈谈HTTP中GET与POST的区别. 2. 在浏览器中 ...
- Matlab学习笔记1—MATLAB基础知识
1.1 MATLAB系统环境 1.MATLAB操作界面的组成 (1)MATLAB主窗口 (2)命令行窗口:命令行窗口用于输入命令并显示命令的执行结果. (3) 当前文件夹窗口 如何设置当前文件夹呢? ...
- 使用ocelot作为api网关
新建网站项目然后添加ocelot 的nuget包 新建ocelot.json的网关的配置文件 { "GlobalConfiguration": { "BaseUrl&qu ...
- Vim命令合集(四)
Vim命令合集 命令历史 以:和/开头的命令都有历史纪录,可以首先键入:或/然后按上下箭头来选择某个历史命令. 启动vim 在命令行窗口中输入以下命令即可 vim 直接启动vim vim filena ...
- [Python3] 025 包
目录 1. 模块 1.1 模块是什么? 1.2 为什么用模块? 1.3 如何定义模块? 1.4 如何使用模块? 1.4.1 例子1 1.4.2 例子2 1.4.3 例子3 1.4.4 例子4 1.4. ...
- Atomic实现原子性源码分析:CAS(比较并交换)、Unsafe类
1.CAS: 比较并交换(Compare And Swap),是Unsafe类中的一条CPU系统原语,原语的执行必须是连续的,在执行过程中不允许被中断,即CAS是一条CPU的原子指令,不会造成数据不一 ...