[React] Fetch Data with React Suspense
Let's get started with the simplest version of data fetching with React Suspense. It may feel a little awkward, but I promise you that you wont be writing your code like this. When Suspense is stable, there will be libraries that integrate with Suspense. But this is approximately what those abstractions will do, so it's a good thing to know.
For a normal React App process, we need to first init component 'PokemonInfo', then we send a fetch request to get data from server.
function App() {
return (
<div className="pokemon-info">
<PokemonInfo />
</div>
)
}
Using Suspense, we don't need to wait component init, we can send request in the very beginning.
To do that, we can wrap the component inside:
<React.Suspense
fallback={
console.log('loading pokemon...') && <div>Loading pokemon...</div>
}
>
<Your-component / >
</React.Suspense>
You need to provide a 'fallback' prop which provide a JXS to rendering during fetching the data.
function App() {
return (
<div className="pokemon-info">
<React.Suspense
fallback={
console.log('loading pokemon...') && <div>Loading pokemon...</div>
}
>
<PokemonInfo />
</React.Suspense>
</div>
)
}
For the data fetching and component rendering:
'pokemonPromise': send fetching request right away, then assign the data to variable 'pokemon'.
Inside component, we just check whether we have 'pokemon' data already, if not, we 'throw poekmonPromise', React Suspense will catch the promise, when it resolves, React will render the component.
let pokemon
let pokemonPromise = fetchPokemon('pikachu').then(p => {
console.log('promise resolve')
pokemon = p
}) function PokemonInfo() {
console.log('PokemonInfo init') 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>
)
}
/*
loading pokemon... // fallback rendering
promise resolve. // data fetched
loading pokemon... // re-rendering the component inside React.Suspense
PokemonInfo init. // component rendered
*/
---
import React from 'react'
import fetchPokemon from '../fetch-pokemon'
import {PokemonDataView} from '../utils' let pokemon
let pokemonPromise = fetchPokemon('pikachu').then(p => {
console.log('promise resolve')
pokemon = p
}) function PokemonInfo() {
console.log('PokemonInfo init') 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">
<React.Suspense
fallback={
console.log('loading pokemon...') && <div>Loading pokemon...</div>
}
>
<PokemonInfo />
</React.Suspense>
</div>
)
} export default App
[React] Fetch Data with React Suspense的更多相关文章
- How to fetch data with React Hooks
Where can I make API call with hooks in react? Async useEffect is pretty much unreadable How to fetc ...
- react & redux data flow diagram
react & redux data flow diagram Redux 数据流程图
- [转] React 最佳实践——那些 React 没告诉你但很重要的事
前言:对很多 react 新手来说,网上能找到的资源大都是些简单的 tutorial ,它们能教会你如何使用 react ,但并不会告诉你怎么在实际项目中优雅的组织和编写 react 代码.用谷歌搜中 ...
- react起步——从零开始编写react项目
# index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...
- 【react学习】关于react框架使用的一些细节要点的思考
( _(:3 」∠)_给园友们提个建议,无论是API文档还是书籍,一定要多看几遍!特别是隔一段时间后,会有意想不到的收获的) 这篇文章主要是写关于学习react中的一些自己的思考: 1.set ...
- React-Native(三):React Native是基于React设计的
React Native是基于React js设计的. 参考:<React 入门实例教程> React 起源于 Facebook 的内部项目,因为该公司对市场上所有 JavaScript ...
- react基础学习和react服务端渲染框架next.js踩坑
说明 React作为Facebook 内部开发 Instagram 的项目中,是一个用来构建用户界面的优秀 JS 库,于 2013 年 5 月开源.作为前端的三大框架之一,React的应用可以说是非常 ...
- React Native 系列(二) -- React入门知识
前言 本系列是基于React Native版本号0.44.3写的,最初学习React Native的时候,完全没有接触过React和JS,本文的目的是为了给那些JS和React小白提供一个快速入门,让 ...
- 1. React介绍 React开发环境搭建 React第一个程序
什么是 React React 是 Facebook 发布的 JavaScript 库,以其高性能和独特的设计理念受到了广泛关注. React的开发背景 Faceboo ...
随机推荐
- SpringCloud Stream使用案例
官方定义 Spring Cloud Stream 是一个构建消息驱动微服务的框架. 应用程序通过 inputs 或者 outputs 来与 Spring Cloud Stream 中binder 交互 ...
- Istio 1.4 更新了 | 感觉学不完
Istio 1.4 更新了 官网 https://istio.io/news/2019/announcing-1.4/ Istio 评选 为GitHub上增长最快的五个 开源项目之一 变更说明获取更改 ...
- 算法设计与分析(李春保)练习题答案v1
1.1第1 章─概论 1.1.1练习题 1.下列关于算法的说法中正确的有(). Ⅰ.求解某一类问题的算法是唯一的 Ⅱ.算法必须在有限步操作之后停止 Ⅲ.算法的每一步操作必须是明确的,不能有歧义或含义模 ...
- FusionInsight大数据开发---HDFS应用开发
HDFS应用开发 HDFS(Dadoop Distributed File System) HDFS概述 高容错性 高吞吐量 大文件存储 HDFS架构包含三部分 Name Node DataNode ...
- SQL分类之DCL:管理用户、授权
DCL:管理用户.授权 SQL分类: DDL:操作数据库和表 DML:增删改表中的数据 DQL:查询表中的数据 DCL:管理用户.授权 DBA:数据库管理员 DCL:管理用户.授权 1.管理用户 1. ...
- C# 单向链表 逆序(递归)
static void Main(string[] args) { while (true) { LinkedList L = new LinkedList(); L.Add(new Node(&qu ...
- WebApi自定义全局异常过滤器及返回数据格式化
WebApi在这里就不多说了,一种轻量级的服务,应用非常广泛.我这这里主要记录下有关 WebApi的相关知识,以便日后使用. 当WebApi应用程序出现异常时,我们都会使用到异常过滤器进行日志记录,并 ...
- wangeditor视频
wangeditor网址http://www.wangeditor.com/ 目前使用的是3.11版本 使用步骤 1.引用wangEditor.min.js 2.代码 2.1 取得函数var E = ...
- Space Syntax(空间句法)
01 December 2019 13:16 https://spacesyntax.com/ 相关软件:Depthmap 空间句法理论作为一种新的描述建筑和城市空间模式的语言 ...
- 任意图像尺寸变成目标尺寸(包含相应的boxes的变换)
def image_preporcess(image, target_size, gt_boxes=None): image = cv2.cvtColor(image, cv2.COLOR_BGR2R ...