前言

看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)的更多相关文章

  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. oracle 12 安装

    oracle 12 第二版本安装 下载地址  https://www.oracle.com/technetwork/cn/middleware/webcenter/content/downloads/ ...

  2. 如图 honehoneclock

    如图人体时钟  honehoneclock 页面演示来自http://chabudai.org/blog/?p=59 <embed name="honehoneclock" ...

  3. FPGA VGA时序的理解

    最近在做FPGA毕业设计,毕业设计规划的是摄像头采集图像,经过均值滤波,中值滤波,高斯滤波,然后通过VGA接口控制显示器显示出来,所以最近学习了一下FPGA的VGA驱动的相关内容. VGA接口 如上图 ...

  4. js 数组去重方法总结

    var arr = [1, 1, 'true', 'true', true, true, 15, 15, false, false, undefined, undefined, null, null, ...

  5. composer配合github发布管理代码包

    前言 今日使用composer结合github管理代码包过程,方便日后需要,特此记录 流程 1 最大同性交友网站github创建自己项目,在自己项目新增composer.json文件 2 compos ...

  6. 关于Vue的一些事

    Vue的官方网站 https://cn.vuejs.org/ Vue中的一些重点 router Vuex 知其然,后知其所以然 这是一篇Vue的源码解读 http://hcysun.me/2017/0 ...

  7. C++中组合和继承的概念及意义

    1,继承在面向对象中具有举足轻重的地位,面向对象当中的很多高级技术都和继承是息息相关的,比如面向对象的高端课程<设计模式>中的每一种技术都和继承有关,因此我们非常有必要在学习 C++ 时, ...

  8. Layui数据表格模型

    视图模型 package com.meiyou.model; import org.springframework.context.annotation.Bean; import java.io.Se ...

  9. Bug快到碗里来

    Bug快到碗里来 python错误--'list' object is not callable 原因及解决方法1 你定义了一个变量的变量名和系统自带的关键字冲突,调用变量时关键字被传到调用的位置,就 ...

  10. AtCoder Beginner Contest 077

    A - Rotation Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement You are g ...