前言

看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. python之reportlab生成PDF文件

    项目需要,需要自动生成PDF测试报告.经过对比之后,选择使用了reportlab模块. 项目背景:开发一个测试平台,供测试维护测试用例,执行测试用例,并且生成测试报告(包含PDF和excel),将生成 ...

  2. 临时产品id记录

    id: 5095552c4fb94e01b37561fac5b20b42 cf51ceb55f5341b78592e8fead31e5c8

  3. 【Deep Learning Nanodegree Foundation笔记】第 5 课:Logistic Regression

    Learn about linear regression and logistic regression models. These simple machine learning models a ...

  4. ubuntu16.04+Titan Xp安装显卡驱动+Cuda9.0+cudnn

    硬件环境 ubuntu 16.04LTS + windows10 双系统 NVIDIA TiTan XP 显卡(12G) 软件环境 搜狗输入法 显卡驱动:LINUX X64 (AMD64/EM64T) ...

  5. python pytorch numpy DNN 线性回归模型

    1.直接奉献代码,后期有入门更新,之前一直在学的是TensorFlow, import torch from torch.autograd import Variable import torch.n ...

  6. SQL修改数据表字段长度

    alter table m_Assysn_t nocheck CONSTRAINT allAlter Table m_Assysn_t ALTER column ppid VARCHAR(150)al ...

  7. kubernetes集群node加入不了master错误处理

    #如果node加入不了master或者加入成功但是,在master中显示不出来.排查错误:1. 运行,kubelet, 查看日志,一般是kubelet的运行和docker启动方式不匹配.调整:vim  ...

  8. Excel透视表进阶之计算字段、计算项、切片器、页面布局

    计算字段 在透视表的字段列表中通过函数.公式等方式构建一个新的字段 又称虚拟字段,因为计算字段不会出现在数据源中,对于普通字段的操作,都可以对计算字段进行操作 计算字段只能出现在值区域,不能出现在筛选 ...

  9. Layui数据表格模型

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

  10. Jade学习(二)之语法规则上

    语法 ⚠️实例均结合node jade缩进代表层级 html <html></html> html <html> head <head> style & ...