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. Delphi微信支付【支持MD5和HMAC-SHA256签名与验签】

    作者QQ:(648437169) 点击下载➨微信支付            微信支付api文档 [Delphi 微信支付]支持付款码支付.二维码支付.订单查询.申请退款.退款查询.撤销订单.关闭订单. ...

  2. django开发_七牛云图片管理

    七牛云注册 https://www.qiniu.com/ 实名认证成功之后,赠送10G存储空间 复制粘贴AK和SK 创建存储空间,填写空间名称,选择存储区域.访问控制选择位公开空间 获取测试域名 七牛 ...

  3. Python 中拼音库 PyPinyin 的用法【华为云技术分享】

    [摘要] 最近碰到了一个问题,项目中很多文件都是接手过来的中文命名的一些素材,结果在部署的时候文件名全都乱码了,导致项目无法正常运行. 后来请教了一位大佬怎么解决文件名乱码的问题,他说这个需要正面解决 ...

  4. Appium中app的元素定位

    app定位方式,本文只讲Android手机的定位方式. 前提条件是adb连接到模拟器或者是手机(具体连接方式这里不再讲解),证明已连接到设备 adb devices app元素定位工具一:UI Aut ...

  5. 关闭Postman 证书的验证

    1.问题背景 使用自己生成的SSL证书,用Postman访问失败.需要忽略SSL证书的验证 2.关闭Postman 证书的验证 在Settings-General中 关闭SSL certificate ...

  6. Mybatis 映射器接口实现类的方式 运行过程debug分析

    查询一张表的所有数据. 环境: 使用工具IntelliJ IDEA 2018.2版本. 创建Maven工程不用骨架 <?xml version="1.0" encoding= ...

  7. AI金融:利用LSTM预测股票每日最高价

    第一部分:从RNN到LSTM 1.什么是RNN RNN全称循环神经网络(Recurrent Neural Networks),是用来处理序列数据的.在传统的神经网络模型中,从输入层到隐含层再到输出层, ...

  8. 8 smali文件格式

    Apktool反编译apk后程序中每一个类都会有一个smali文件. 一.当前类信息:smali文件的头三行描述了当前类的一些信息. Eg: .class <访问权限> [修饰关键字]&l ...

  9. Java集合学习(8):LinkedList

    一.概述 LinkedList和ArrayList一样,都实现了List接口,但其内部的数据结构有本质的不同.LinkedList是基于链表实现的(通过名字也能区分开来),所以它的插入和删除操作比Ar ...

  10. Linux装B命令

    原文:https://mp.weixin.qq.com/s/CNmMRjl0iZ8EBPq5VgJHsA 1.空心字体 yum install -y figlet figlet happy 1.0 2 ...