Error Boundaries are the way you handle errors with React, and Suspense embraces this completely. Let's take a look at how to handle asynchronous errors with Suspense and Error Boundaries.

In previous post, we used React.Suspense with fallback (for loading..), in this post, we will see how to handle error case with ErrorBoundary. https://reactjs.org/docs/error-boundaries.html

NPM module: https://npm.im/react-error-boundary

An ErrorBoundary component:

// utils.js

class ErrorBoundary extends React.Component {
state = {error: null}
static getDerivedStateFromError(error) {
return {error}
}
componentDidCatch() {
// log the error to the server
}
tryAgain = () => this.setState({error: null})
render() {
return this.state.error ? (
<div>
There was an error. <button onClick={this.tryAgain}>try again</button>
<pre style={{whiteSpace: 'normal'}}>{this.state.error.message}</pre>
</div>
) : (
this.props.children
)
}
}

---

import React from 'react'
import fetchPokemon from '../fetch-pokemon'
import {PokemonDataView, ErrorBoundary} from '../utils' let pokemon
let pokemonError
let pokemonPromise = fetchPokemon('pikachue').then(
p => {
console.log('promise resolve')
pokemon = p
},
e => {
pokemonError =
e
},

) function PokemonInfo() {
console.log('PokemonInfo init') if (pokemonError) {
throw pokemonError
}
if (!pokemon) {
throw pokemonPromise // this API might change
} return (
<div>
<div className="pokemon-info__img-wrapper">
<img src={pokemon.image} alt={pokemon.name} />
</div>
<PokemonDataView pokemon={pokemon} />
</div>
)
} function App() {
return (
<div className="pokemon-info">
<ErrorBoundary>
<React.Suspense
fallback={
console.log('loading pokemon...') && <div>Loading pokemon...</div>
}
>
<PokemonInfo />
</React.Suspense>
</ErrorBoundary>
</div>
)
} export default App

[React] Handle React Suspense Errors with an Error Boundary的更多相关文章

  1. React源码 Suspense 和 ReactLazy

    React 16.6 提供的一个新的开放一部分功能的 Suspense 代码 import React, { Suspense, lazy } from 'react' const LazyComp ...

  2. JavaScript 和 React,React用了大量语法糖,让JS编写更方便。

    https://reactjs.org/docs/higher-order-components.htmlhttps://codepen.io/gaearon/pen/WooRWa?editors=0 ...

  3. 《React Native 精解与实战》书籍连载「React 与 React Native 简介」

    此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...

  4. [React] Understand React.Children Utilities

    The data contained in this.props.children is not always what you might expect. React provides React. ...

  5. react之react Hooks

    函数组件,没有 class 组件中的 componentDidMount.componentDidUpdate 等生命周期方法,也没有 State,但这些可以通过 React Hook 实现. Rea ...

  6. React学习笔记-1-什么是react,react环境搭建以及第一个react实例

    什么是react?react的官方网站:https://facebook.github.io/react/下图这个就是就是react的标志,非常巧合的是他和我们的github的编辑器Atom非常相似. ...

  7. 小谈React、React Native、React Web

    React有三个东西,React JS 前端Web框架,React Native 移动终端Hybrid框架,React Web是一个源码转换工具(React Native 转 Web,并之所以特别提出 ...

  8. React的React Native

    React的React Native React无疑是今年最火的前端框架,github上的star直逼30,000,基于React的React Native的star也直逼20,000.有了React ...

  9. React Navigation & React Native & React Native Navigation

    React Navigation & React Native & React Native Navigation React Navigation https://facebook. ...

随机推荐

  1. QT之类型转换

    Qt在进行数据类型转换时,容易忘记如何使用,或者是早已厌倦了百度QString转QByteArray,QByteArray转char,QString转string....... 现在分享一篇QT数据类 ...

  2. SQL小技巧(一)拼音首字母的模糊查询

    基于Microsoft SQL Server 的标量值函数fun_GetPy,借鉴其他优秀的博主文章,此处贴出源码,以及使用方法 1.打开新建查询,贴如下代码,F5 /****** Object: U ...

  3. .Net Core WebAPI开启静态页,设置主页

    1.使用场景 默认创建的.Net Core WebAPI应用在运行时是没有页面显示的,效果如下: 那么,如果想要给API设置一个主页,应该怎么做呢?这就需要用到本文提供的方法. 2.设置方法 (1)首 ...

  4. centos从零开始安装elasticSearch

    前言:elasticSearch作为一款优秀的分布式搜索工具,被广泛用在数据搜集和整理的业务中,知名的比如有github就是采用es来精准的搜索几千万行代码,百度也大量应用es做数据爬取分析,本篇博客 ...

  5. Spring Security 解析(七) —— Spring Security Oauth2 源码解析

    Spring Security 解析(七) -- Spring Security Oauth2 源码解析   在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因 ...

  6. 安装fastnlp

    直接 pip install fastnlp 如果出现 torch的安装报错,可能与操作系统及 CUDA 的版本相关.直接上pytorch 的官网 https://pytorch.org/get-st ...

  7. python的new与init

    基于文章:Why is init() always called after new()? 特别说明: 这篇文章的灵感来源于stackoverflow的一个提问,准确说,我只是做了些知识梳理的工作,对 ...

  8. [摘录]flutter打包后无法访问接口

    打开文件{{flutterPorject}}\android\app\src\main\AndroidManifest.xml这个文件增加权限信息: <uses-permission andro ...

  9. web和网络基础

    TCP/IP 协议族按层次分别分为以下 4 层: 应用层. 传输层. 网络层和数据链路层 把 TCP/IP 层次化是有好处的. 比如, 如果互联网只由一个协议统筹, 某个地方需要改变设计时, 就必须把 ...

  10. 从零开始搭建vue+element-ui后台管理系统项目到上线

    前言 之前有些过移动端的项目搭建的文章,感觉不写个pc端管理系统老感觉少了点什么,最近公司项目比较多,恰巧要做一个申报系统的后台管理系统,鉴于对vue技术栈比较熟悉,所以考虑还是使用vue技术栈来做: ...