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. vivado sdx 綜合報錯ERROR: [SdsCompiler 83-5019] Exiting sds++ : Error when calling 'aarch64-linux-gnu-g++ -c

    ERROR: [SdsCompiler 83-5019] Exiting sds++ : Error when calling 'aarch64-linux-gnu-g++ -c 主函數 添加 頭文件 ...

  2. Prometheus 标签使用示例整合

    Prometheus 监控实例 一.Prometheus 根据标签聚合总CPU使用率 1.主机添加标签(可在多个主机内添加相同标签实现聚合):vim prometheus.conf static_co ...

  3. Git系列 —— 记一次Mac上git push时总是403的错误

    问题: 今天从github上clone下一个项目,然后修改后git push时总是出现: remote:Permission to lixyou/rw-split-plugin.git defined ...

  4. Elasticsearch常见用法-分布式集群

    集群内部工作方式 Elasticsearch用于构建高可用和可扩展的系统.扩展的方式可以是购买更好的服务器(纵向扩展(vertical scale or scaling up))或者购买更多的服务器( ...

  5. Android开发DDMS找不到Emulator Control的方法

    1.右键DDMS,点击reset. 2.window->show view->other->android->Emulator Control

  6. 接口例_龟车赛跑_Java

    此例演示java中接口的一般用法. 屋子里有一群程序员,每个人在写着自己的类,共同构建一个世界. 项目经理突然出现:“打扰大家一下,公司决定举办一个竞速比赛,你们写的类都可以参加.为了比赛的顺利进行, ...

  7. Vertx的命令行

    IntelliJ----创建一个运行配置(Application), 用io.vertx.core.Launcher类作为主类,在程序参数输入:run your-verticle-fully-qual ...

  8. Spring Security实现OAuth2.0授权服务 - 进阶版

    <Spring Security实现OAuth2.0授权服务 - 基础版>介绍了如何使用Spring Security实现OAuth2.0授权和资源保护,但是使用的都是Spring Sec ...

  9. Jemeter学习环境部署。

    本文档中所有软件的下载地址 链接:https://pan.baidu.com/s/1RREUwlH7GtYMUWeiRjtWVg 提取码:zmjy 一.安装jdk 下载网盘中的jdk 双击jdk-8u ...

  10. expor和import的用法

    1.Export 模块是独立的文件,该文件内部的所有的变量外部都无法获取.如果希望获取某个变量,必须通过export输出 // profile.js export var firstName = 'M ...