[React] Handle React Suspense Errors with an Error Boundary
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的更多相关文章
- React源码 Suspense 和 ReactLazy
React 16.6 提供的一个新的开放一部分功能的 Suspense 代码 import React, { Suspense, lazy } from 'react' const LazyComp ...
- JavaScript 和 React,React用了大量语法糖,让JS编写更方便。
https://reactjs.org/docs/higher-order-components.htmlhttps://codepen.io/gaearon/pen/WooRWa?editors=0 ...
- 《React Native 精解与实战》书籍连载「React 与 React Native 简介」
此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...
- [React] Understand React.Children Utilities
The data contained in this.props.children is not always what you might expect. React provides React. ...
- react之react Hooks
函数组件,没有 class 组件中的 componentDidMount.componentDidUpdate 等生命周期方法,也没有 State,但这些可以通过 React Hook 实现. Rea ...
- React学习笔记-1-什么是react,react环境搭建以及第一个react实例
什么是react?react的官方网站:https://facebook.github.io/react/下图这个就是就是react的标志,非常巧合的是他和我们的github的编辑器Atom非常相似. ...
- 小谈React、React Native、React Web
React有三个东西,React JS 前端Web框架,React Native 移动终端Hybrid框架,React Web是一个源码转换工具(React Native 转 Web,并之所以特别提出 ...
- React的React Native
React的React Native React无疑是今年最火的前端框架,github上的star直逼30,000,基于React的React Native的star也直逼20,000.有了React ...
- React Navigation & React Native & React Native Navigation
React Navigation & React Native & React Native Navigation React Navigation https://facebook. ...
随机推荐
- Java自学-类和对象 类属性
Java的类属性和对象属性 当一个属性被static修饰的时候,就叫做类属性,又叫做静态属性 当一个属性被声明成类属性,那么所有的对象,都共享一个值 与对象属性对比: 不同对象的 对象属性 的值都可能 ...
- Oracle查询所有字段另加两个拼接字段的操作
Oracle查询所有字段,再加两个字段拼接, select a.*,(SNO||SNAME) from TEST_STUDENT a; 同理,查询所有字段,其中两个字段求和:(SNO和SAGE都是NU ...
- - Java中boolean类型占用多少个字节 MD
目录 目录 Java中boolean类型占用多少个字节 1个bit(1位) 1个Byte(1字节,8位) 4个Byte(4字节,32位) 分析 官方文档中的描述 Markdown版本笔记 我的GitH ...
- final,finally,finalize之间的区别。
fianl:可以修饰类.变量.方法.修饰类不能被继承,修饰变量只能赋值一次,修饰方法不能被重写. finally是try语句体中的一个语句体,不能单独使用,用来释放资源. finalize()是在ja ...
- 分布式事务:Saga模式
1 Saga相关概念 1987年普林斯顿大学的Hector Garcia-Molina和Kenneth Salem发表了一篇Paper Sagas,讲述的是如何处理long lived transac ...
- 英语insuraunce保险insuraunce单词
English Alternative forms insuraunce Etymology From the older form ensurance, see also assurance. Pr ...
- mysql 查询当天数据
查询当天数据 select * from tab where FROM_UNIXTIME(fabutime, '%Y%m%d') = 20121217; mysql TO_DAYS(date) 函 ...
- java虚拟机类加载机制和双亲委派模型
java虚拟机类加载机制:虚拟机把描述类的数据从class文件加载到内存,并对数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的java类型. 类的生命周期是从类被加载到虚拟机内存中,到卸 ...
- MongoDB配置文件及添加用户
一.参数启动mongodb $ mongod --bind_ip=0.0.0.0 二.配置文件: mongod.conf # 后台运行 fork=true # 数据存储文件目录 dbpath=/roo ...
- 【前端_js】Bootstrap之表单验证
Bootstrap表单验证插件bootstrapValidator使用方法整理 BootstrapValidator 表单验证超详细教程