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. java实现二维码的生成和解读

    Java利用QRCode.jar包实现二维码编码与解码   QRcode是日本人94年开发出来的.首先去QRCode的官网http://swetake.com/qrcode/java/qr_java. ...

  2. play framework + sbt入门之环境搭建

    一 sbt的使用 SBT = (not so) Simple Build Tool,是scala的构建工具,与java的maven地位相同.其设计宗旨是让简单的项目可以简单的配置,而复杂的项目可以复杂 ...

  3. Java学习:switch语句使用的注意事项

    switch语句使用的注意事项: 多个case后面的数值不可以重复. switch后面的小括当中只能是下列数据类型: 基本数据类型:byte/short/char/int 引用数据类型:String字 ...

  4. Mysql系列(四) —— MySQL的Charset和Collation

    本文转载自:再见乱码:5分钟读懂MySQL字符集设置 一.内容概述 在MySQL的使用过程中,了解字符集.字符序的概念,以及不同设置对数据存储.比较的影响非常重要.不少同学在日常工作中遇到的" ...

  5. 【已解決】谷歌浏览器如何清理缓存(cookie)

    清除缓存快捷键 Ctrl+Shift+Delete

  6. delphi 格式转换

    TO_CHAR 是把日期或数字转换为字符串 TO_DATE 是把字符串转换为数据库中得日期类型转换函数TO_NUMBER 将字符转化为数字 TO_CHAR 使用TO_CHAR函数处理数字 TO_CHA ...

  7. Java自学-数字与字符串 格式化输出

    Java 使用printf或format 进行格式化输出 步骤 1 : 格式化输出 如果不使用格式化输出,就需要进行字符串连接,如果变量比较多,拼接就会显得繁琐 使用格式化输出,就可以简洁明了 %s ...

  8. python json库

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写. 1.json库的使用 使用 JSON 函数需要导入 json 库:import jso ...

  9. 为什么K8s会成为主流?

    容器技术和K8s是云原生概念的核心和基础.云计算诞生已有超过10年,但云计算时代的应用到底该是什么样子,一直没人能说清楚,也没人能确定云计算的基础架构将会如何发展.在K8s出现之前,没人设想过会有一个 ...

  10. php curl 转为 x-www-form-urlencoded 方式的坑

    网上转变的方法基本都是写添加下面这句: curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-fo ...