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. tornado之websoket

    继承WebSoketHandler def open(self): # 当一个WebSoket连接建立之后被调用 def on_message(self, message): # 当客户端发送一个消息 ...

  2. 【mysql】获取某个表所有列名【mybatis】

    方法1:[仅指定表名] select COLUMN_NAME from information_schema.COLUMNS where table_name = 'your-table-name'; ...

  3. java异常的嵌套和级联

    一.分开捕获或者嵌套使用 我们先看看下面这段代码: public class Cal { public int div(int a, int b) { int result = a / b; retu ...

  4. 【C#常用方法】3.将DataTable一次性插入数据库表中(使用SqlBulkCopy)

    将DataTable一次性插入数据库表中(使用SqlBulkCopy) 1.SqlBulkCopy简介 SqlBulkCopy类是ADO.NET中专门用于数据库批量插入数据的类,其批量插入的执行速度是 ...

  5. js-beautify 不换行

    最近在用Hbuilder-X,自带js-beautify.但是默认格式化设置实在是看着太难受.来看看一个VX的格式化,参数愣是给用了三行才格式化完,而且两个参数还分开了,看着太难受. // mutat ...

  6. python 读取.mat文件

    导入所需包 from scipy.io import loadmat 读取.mat文件 随便从下面文件里读取一个: m = loadmat('H_BETA.mat') # 读出来的 m 是一个dict ...

  7. 一篇文章搞定redis

    Redis 简介 Redis 是完全开源免费的,遵守 BSD 协议,是一个高性能的 key - value 数据库 Redis 与 其他 key - value 缓存产品有以下三个特点: Redis ...

  8. 单词canutillos祖母绿canutillos英语

    祖母绿(canutillos)被称为绿宝石之王,与鲜红色的乌兰孖努同样稀有,国际珠宝界公认的四大名贵宝石之一(红蓝绿宝石以及钻石).因其特有的绿色和独特的魅力,以及神奇的传说,深受西方人的青睐. 祖母 ...

  9. mysql.cnf配置文件详解

    参数详解 [client] #客户端设置,即客户端默认的连接参数port = 3307   #默认连接端口socket = /data/mysqldata/3307/mysql.sock #用于本地连 ...

  10. NN入门

    参考资料:https://blog.csdn.net/kwame211/article/details/77337166, 仅作为个人学习笔记.人工智能的底层模型是"神经网络"(n ...