前言

看react 文档突然发现有这个 错误处理函数,好像是17年9月出的,这个真的绝了可以帮助我们捕捉错误咯

React 16 将提供一个内置函数 componentDidCatch,如果 render() 函数抛出错误,则会触发该函数。

官网例子

下面这个:

  1. class ErrorBoundary extends React.Component {
  2. constructor(props) {
  3. super(props);
  4. this.state = { hasError: false };
  5. }
  6.  
  7. componentDidCatch(error, info) {
  8. // Display fallback UI
  9. this.setState({ hasError: true });
  10. // You can also log the error to an error reporting service
  11. logErrorToMyService(error, info);
  12. }
  13.  
  14. render() {
  15. if (this.state.hasError) {
  16. // You can render any custom fallback UI
  17. return <h1>Something went wrong.</h1>;
  18. }
  19. return this.props.children;
  20. }
  21. }

当然你可以把这个组件封装下成为

  1. <ErrorBoundary>
  2. <MyWidget />
  3. </ErrorBoundary>

然后在顶部或任何地方,你可以这样使用它

另一个特性:

componentDidCatch 是包含错误堆栈的 info 对象!

{this.state.info && this.state.info.componentStack}

当然我是这么用的在路由那边

  1. class App extends React.Component {
  2. constructor(props) {
  3. super(props)
  4. this.state = {
  5. hasError: false
  6. }
  7. }
  8. componentDidCatch(error, info) {
  9. console.log(error, info)
  10. this.setState({
  11. hasError: true
  12. })
  13. }
  14. render() {
  15. return this.state.hasError ?
  16. <h2>页面出错了404</h2>
  17. : (
  18. <React.Fragment>
  19. {/* 检验是否有登录信息 */}
  20. <AutoRoute />
  21. {/* 有了switch后,匹配到path后就不会再匹配下去了 */}
  22. <Switch>
  23. <Route path="/login" component={Login}></Route>
  24. <Route path='/register' component={Register}></Route>
  25. <Route path='/chat/:user' component={Chat}></Route>
  26. </Switch>
  27. </React.Fragment>
  28. )
  29. }
  30. }

其实还是组件化比较好一点,毕竟复用性比较高

React 错误处理(componentDidCatch)的更多相关文章

  1. React 错误边界组件

    这是React16的内容,并不是最新的技术,但是用很少被讨论,直到通过文档发现其实也是很有用的一部分内容,还是总结一下- React中的未捕获的 JS 错误会导致整个应用的崩溃,和整个组件树的卸载.从 ...

  2. React错误总结解决方案(二)

    1.React native: Cannot add a child that doesn't have a YogaNode or parent node 该错误一般是因为render方法中注释语句 ...

  3. webpack react 错误整理

    1.ERROR in ./src/entry.js Module build failed: SyntaxError 解决方法: 安装babel-preset-react,  npm install ...

  4. React错误总结(三)

    神坑react native之Cannot Add a child that doesn't have a YogaNode to a parent with out a measure functi ...

  5. react错误总结

    react-native 错误总结 The development server returned response error code: 500 in react-native https://b ...

  6. 一个自己犯的react错误

    在看<react小书>高阶组件一节的时候,看到如下代码 import React, { Component } from 'react' export default (WrappedCo ...

  7. React错误收集

    1.  Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/ ...

  8. react 错误处理

    https://www.jianshu.com/p/61d09e488743 https://codepen.io/sgroff04/pen/dVbgJy/

  9. react那些事儿

    一.参考链接https://reactjs.org/http://react-china.org/https://doc.react-china.org/https://hulufei.gitbook ...

随机推荐

  1. 二十九:数据库之SQLAlchemy连接数据库

    手写SQL复用性不高,SQL越复杂越容易出bug,而且容易引起SQL注入,所以现在Ibanez使用orm框架将数据库映射为数据模型,这是使用SQLAlchemy 准备工作,安装SQLAlchemy和P ...

  2. RabbitMQ问题解决

    1.访问http://localhost:15672/#/,输入用户名.密码登录报错500 解决方法:在快捷程序处找到RabbitMQ Service -stop停止服务,然后再点击RabbitMQ ...

  3. Elasticsearch 为何要在 7.X版本中 去除type 的概念

    背景说明 Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎.无论在开源还是专有领域,Lucene可以被认为是迄今为止最先进.性能最好的.功能最全的搜索引擎库. El ...

  4. dapper使用时性能优化

    数据库中类型 Area 数据库类型 varchar dapper 来操作数据库,不能直接写 sql      Area=@Area)   //dapper 对C#中的字符串类型 默认是对应数据库nva ...

  5. GET 和 POST 区别?网上多数答案都是错的!

    最近在看<HTTP权威指南>这本书,对HTTP协议有了更深一层的了解. 在我们面试过程中关于HTTP协议有两个经典的面试题: 1. 谈谈HTTP中GET与POST的区别. 2. 在浏览器中 ...

  6. Matlab学习笔记1—MATLAB基础知识

    1.1  MATLAB系统环境 1.MATLAB操作界面的组成 (1)MATLAB主窗口 (2)命令行窗口:命令行窗口用于输入命令并显示命令的执行结果. (3) 当前文件夹窗口 如何设置当前文件夹呢? ...

  7. 使用ocelot作为api网关

    新建网站项目然后添加ocelot 的nuget包 新建ocelot.json的网关的配置文件 { "GlobalConfiguration": { "BaseUrl&qu ...

  8. Vim命令合集(四)

    Vim命令合集 命令历史 以:和/开头的命令都有历史纪录,可以首先键入:或/然后按上下箭头来选择某个历史命令. 启动vim 在命令行窗口中输入以下命令即可 vim 直接启动vim vim filena ...

  9. [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. ...

  10. Atomic实现原子性源码分析:CAS(比较并交换)、Unsafe类

    1.CAS: 比较并交换(Compare And Swap),是Unsafe类中的一条CPU系统原语,原语的执行必须是连续的,在执行过程中不允许被中断,即CAS是一条CPU的原子指令,不会造成数据不一 ...