[React] Write a generic React Suspense Resource factory
Using Suspense within our component isn't exactly ergonomic. Let's put all that logic into a reusable function so we can create resources anytime we need them and for any asynchronous interaction in our app.
In previous post, https://www.cnblogs.com/Answer1215/p/12006526.html, we have see how to use React.Suspense to handle data fetching, with fallback and ErrorBoundary.
In this post, we will refactor code to make a generic function to handle all use cases.
function createFetch(fetchFn) {
let status = 'pending'
let result
let error
let promise = fetchFn().then(
p => {
console.log('promise resolve')
status = 'success'
result = p
},
e => {
status = 'error'
error = e
},
)
return {
read() {
if (status === 'error') {
throw error
}
if (status === 'pending') {
throw promise // this API might change
}
if (status === 'success') {
return result
}
},
}
}
Use:
const promise = createFetch(() => fetchPokemon('pikachu'))
function PokemonInfo() {
console.log('PokemonInfo init')
const pokemon = promise.read()
return (
<div>
<div className="pokemon-info__img-wrapper">
<img src={pokemon.image} alt={pokemon.name} />
</div>
<PokemonDataView pokemon={pokemon} />
</div>
)
}
[React] Write a generic React Suspense Resource factory的更多相关文章
- angular的$resource factory都有啥
angular的$resource factory都有啥 A factory which creates a resource object that lets you interact with R ...
- Could not load resource factory class [Root exception is java.lang.ClassNotFoundException: org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory]
WARNING: Failed to register in JMX: javax.naming.NamingException: Could not load resource factory cl ...
- 【react学习】关于react框架使用的一些细节要点的思考
( _(:3 」∠)_给园友们提个建议,无论是API文档还是书籍,一定要多看几遍!特别是隔一段时间后,会有意想不到的收获的) 这篇文章主要是写关于学习react中的一些自己的思考: 1.set ...
- React Native 系列(二) -- React入门知识
前言 本系列是基于React Native版本号0.44.3写的,最初学习React Native的时候,完全没有接触过React和JS,本文的目的是为了给那些JS和React小白提供一个快速入门,让 ...
- React-Native(三):React Native是基于React设计的
React Native是基于React js设计的. 参考:<React 入门实例教程> React 起源于 Facebook 的内部项目,因为该公司对市场上所有 JavaScript ...
- 1. React介绍 React开发环境搭建 React第一个程序
什么是 React React 是 Facebook 发布的 JavaScript 库,以其高性能和独特的设计理念受到了广泛关注. React的开发背景 Faceboo ...
- [React] 02 - Intro: why react and its design pattern
为啥使用React,给我个理由 过去 需要手动更新DOM.费力地记录每一个状态:既不具备扩展性,又很难加入新的功能,就算可以,也是有着冒着很大的风险. 不过,使用这种开发方式很难打造出极佳的用户体验. ...
- 转载 React.createClass 对决 extends React.Component
先给出结论,这其实是殊途同归的两种方式.过去我们一般都会使用 React.createClass 方法来创建组件,但基于 ES6 的小小语法糖,我们还可以通过 extends React.Compon ...
- 【React 资料备份】React Hook
Hooks是React16.8一个新增项,是我们可以不用创建class组件就能使用状态和其他React特性 准备工作 升级react.react-dom npm i react react-dom - ...
随机推荐
- Apache Kafka - How to Load Test with JMeter
In this article, we are going to look at how to load test Apache Kafka, a distributed streaming plat ...
- 『Blocks 区间dp』
Blocks Description Some of you may have played a game called 'Blocks'. There are n blocks in a row, ...
- C# 多维数组 交错数组的区别,即 [ , ] 与 [ ][ ]的区别 (转载)
多维数组的声明 在声明时,必须指定数组的长度,格式为 type [lenght ,lenght ,lengh, ... ] , ]; 或声明时即赋值,由系统推断长度 int [,] test1 = { ...
- Windows下载安装RabbitMQ教程
原文链接:http://www.studyshare.cn/software/details/1171/0一.下载 1.下载Erlang 官网下载:去下载 百度网盘下载:去下载 提取码:m1q0 2 ...
- Django-xadmin的使用介绍
Django-xadmin的介绍 Django是python的重量级web框架,写得少,做得多,非常适合后端开发,它很大的一个亮点是,自带后台管理模块,但它自带的后台管理有点丑,而Xadmin是基于b ...
- Prime Path POJ-3126
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that ...
- rem适配移动端
一.屏幕宽度 / 设计稿宽度 *100 来设置根元素的 font-size 10px = 0.10rem (function (doc, win) { var docEl = doc.docume ...
- OCR3:tesseract script
通过命令:tesseract -h 可查看 OCR操作脚本参数: 其中参数说明: –-oem:指定使用的算法,0:代表老的算法:1:代表LSTM算法:2:代表两者的结合:3:代表系统自己选择. –-p ...
- Fiddler抓包工具简介
1.Fiddler工具: 个人认为Fiddler实际上是一款在浏览器与服务器之间设置代理,对两者之间的通信会话(数据包)进行抓取和处理的一款工具: Fiddler是一个http协议调试代理工具,它能够 ...
- ORA-12514: 监听程序当前无法识别连接描述符中请求的服务
/** 异常:ORA-12514: 监听程序当前无法识别连接描述符中请求的服务 * 背景:在很长一段时间都在连接远程开发库,曾偶尔有一次想要连接本地的库进行sql测试,发现连接失败,起初一直有无监听. ...