前言

看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. JavaScript编程精解 初读笔记

    1.1 值 JavaScript里有6种基本类型的值: number.string.Boolean.object.function和undefined. 1.3.2 prompt和confirm 浏览 ...

  2. :成功配置 centos + nginx + .net core 2.0

    https://segmentfault.com/a/1190000010763523

  3. activiti随笔记录

    核心组件介绍 关键对象 1.      Deployment:流程部署对象,部署一个流程时创建. 2.      ProcessDefinitions:流程定义,部署成功后自动创建. 3.       ...

  4. Windows 上第一款全局轮盘菜单软件(鼠标党进)

    哈哈,我又来了. 如果之前已经有人做过了类似的,估计也没我做的好,反正我是没有见到过的

  5. 意想不到的JavaScript(每日一题2)

    问题一: 答案: 解析:

  6. ubuntu18.04安装Vulhub

    环境 虚拟机vmware ubuntu18.04 已安装docker 1.安装docker-compose 前提:Docker-compose基于Python开发,需要pip Docker-compo ...

  7. 【miscellaneous】网络摄像机

    自20世纪90年代初期网络摄像机开始诞生,产业已历经20余年的演变. "IP大时代"的口号在安防领域已响彻已久,但也是自2015年至今才开使有了真正的底气.当全面超越模拟已尘埃落定 ...

  8. vue,基于element的tree组件封装

    封装组件代码 // 组件:树 /* 参数说明-属性: 1.treeData:展示数据(array) 2.treeEmptyText:内容为空的时候展示的文本(String) 3.treeNodeKey ...

  9. linux netstat 查看端口

    1. netstat命令用于显示系统的网络信息,包括网络连接 .路由表 .接口状态2. 一般我们使用 netstat 来查看本机开启了哪些端口,查看有哪些客户端连接 [root@localhost ~ ...

  10. C# 字符串、字节数组互相转换

    /// <summary> /// MD5加密 /// </summary> /// <param name="sender"></par ...