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的更多相关文章

  1. 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 ...

  2. react & redux data flow diagram

    react & redux data flow diagram Redux 数据流程图

  3. [转] React 最佳实践——那些 React 没告诉你但很重要的事

    前言:对很多 react 新手来说,网上能找到的资源大都是些简单的 tutorial ,它们能教会你如何使用 react ,但并不会告诉你怎么在实际项目中优雅的组织和编写 react 代码.用谷歌搜中 ...

  4. react起步——从零开始编写react项目

    # index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...

  5. 【react学习】关于react框架使用的一些细节要点的思考

    ( _(:3 」∠)_给园友们提个建议,无论是API文档还是书籍,一定要多看几遍!特别是隔一段时间后,会有意想不到的收获的)   这篇文章主要是写关于学习react中的一些自己的思考:   1.set ...

  6. React-Native(三):React Native是基于React设计的

    React Native是基于React js设计的. 参考:<React 入门实例教程> React 起源于 Facebook 的内部项目,因为该公司对市场上所有 JavaScript ...

  7. react基础学习和react服务端渲染框架next.js踩坑

    说明 React作为Facebook 内部开发 Instagram 的项目中,是一个用来构建用户界面的优秀 JS 库,于 2013 年 5 月开源.作为前端的三大框架之一,React的应用可以说是非常 ...

  8. React Native 系列(二) -- React入门知识

    前言 本系列是基于React Native版本号0.44.3写的,最初学习React Native的时候,完全没有接触过React和JS,本文的目的是为了给那些JS和React小白提供一个快速入门,让 ...

  9. 1. React介绍 React开发环境搭建 React第一个程序

    什么是 React         React 是 Facebook 发布的 JavaScript 库,以其高性能和独特的设计理念受到了广泛关注. React的开发背景         Faceboo ...

随机推荐

  1. Excel批量添加不同的批注

    Sub 批量添加不同批注() Dim rng As Range Dim i As String Range("A1:D1").ClearComments For Each rng ...

  2. 怎么看系统是UEFI还是Legacy BIOS启动模式?

    在命令行 cmd 中输入  msinfo32 ,找到右侧[BIOS模式],看到这里显示的是[uefi],那么说明是[uefi]方式启动的,反之显示为[BIOS],那么就是传统[BIOS]启动模式.如下 ...

  3. HDU校赛 | 2019 Multi-University Training Contest 5

    2019 Multi-University Training Contest 5 http://acm.hdu.edu.cn/contests/contest_show.php?cid=852 100 ...

  4. 【题解】Luogu P5471 [NOI2019]弹跳

    原题传送门 先考虑部分分做法: subtask1: 暴力\(O(nm)\)枚举,跑最短路 subtask2: 吧一行的点压到vector中并排序,二分查找每一个弹跳装置珂以到达的城市,跑最短路 sub ...

  5. Vert.x(vertx)发送 HTTP/HTTPS请求

    Vert.x Web服务有两种协议,一种是HTTP,另外一种是使用ssl的HTTPS,请求的方式有五种,分别是get.post.put.delete.head.为了简单,服务端主要实现对HTTP协议的 ...

  6. k-匿名算法

    30 November 2019 18:31     人类历史上,除了计算机外从没有一项技术可以在短短的几十年间,能够全方位的影响整个社会的各个领域.技术的发展,少不了许多代人为之的努力.无论是在计算 ...

  7. Python网络编程、爬虫之requests模块使用

    一.python操作网络,也就是打开一个网站,或者请求一个http接口,使用urllib模块. urllib模块是一个标准模块,直接import urllib即可,在python3里面只有urllib ...

  8. springboot使用 @Transactional 注解配置事务管理

    介绍 springboot对数据库事务的使用非常的方便,只需要在方法上添加@Transactional注解即可.Spring 为事务管理提供了丰富的功能支持.Spring 事务管理分为编程式和声明式的 ...

  9. [转发] SAP EPIC 银企直连+TRM资金管理

    事务代码:EPIC_PROC 电子支付集成 收款; 付款; 付款审批; 银行回单(下载,创建,修改,辩识,认领); 查询账户余额; 查询交易明细; BADI增强; VA虚拟账户客户回单自动辨识; .. ...

  10. Beego 学习比较8:SQL语句

    SQL语句 1>     常用的SQL语句 1->新增  insert into 表名(字段A,字段B,…) Values(字段A值,字段B值,…) 2->更新  update 表名 ...