Based on research at Facebook, we know that if a user sees a flash of loading state, they perceive the app as being slower. So let's improve the pending experience for users with faster connections using css transitions to avoid showing the loading state right away.

import React from 'react'
import fetchPokemon from '../fetch-pokemon'
import {
ErrorBoundary,
createResource,
PokemonInfoFallback,
PokemonForm,
PokemonDataView,
} from '../utils' function PokemonInfo({pokemonResource}) {
const pokemon = pokemonResource.read()
return (
<div>
<div className="pokemon-info__img-wrapper">
<img src={pokemon.image} alt={pokemon.name} />
</div>
<PokemonDataView pokemon={pokemon} />
</div>
)
} const SUSPENSE_CONFIG = {timeoutMs: 4000} function createPokemonResource(pokemonName) {
return createResource(() => fetchPokemon(pokemonName))
} function App() {
const [pokemonName, setPokemonName] = React.useState('')
const [startTransition, isPending] = React.useTransition(SUSPENSE_CONFIG)
const [pokemonResource, setPokemonResource] = React.useState(null) function handleSubmit(newPokemonName) {
setPokemonName(newPokemonName)
startTransition(() => {
setPokemonResource(createPokemonResource(newPokemonName))
})
} return (
<div>
<PokemonForm onSubmit={handleSubmit} />
<hr />
<div className={`pokemon-info ${isPending ? 'pokemon-loading' : ''}`}>
{pokemonResource ? (
<ErrorBoundary>
<React.Suspense
fallback={<PokemonInfoFallback name={pokemonName} />}
>
<PokemonInfo pokemonResource={pokemonResource} />
</React.Suspense>
</ErrorBoundary>
) : (
'Submit a pokemon'
)}
</div>
</div>
)
} export default App
.pokemon-info.pokemon-loading {
opacity: 0.6;
transition: opacity 0s;
/* note: the transition delay is the same as the busyDelayMs config */
transition-delay: 0.4s;
}

[React] Use CSS Transitions to Avoid a Flash of Loading State的更多相关文章

  1. CSS transitions深入理解

    到底css transition是什么,我们来看w3c的解释: CSS Transitions allow property changes in CSS values to occur smooth ...

  2. CSS: transitions

    CSS Transitions CSS transitions allows you to change property values smoothly (from one value to ano ...

  3. React中css的使用

    网页的布局.颜色.形状等UI展示方式主要是由Css进行设置,在ReactJs中也是一样.ReactJs中的Css结构方式与传统的Web网页类似,但依然存在一些差异.ReactJs中Css文件本身的编写 ...

  4. React Transition css动画案例解析

    实现React Transition Css动画效果 首先在项目工程中引入react-transition-group: npm install react-transition-group --sa ...

  5. Flash网站Loading制作

    Flash网站Loading制作~~~ stop(); stage.scaleMode=StageScaleMode.NO_SCALE; //指定舞台属性为不跟随播放器大小而改变 stage.show ...

  6. create-react-app创建react项目 css模块化处理

    用的css预处理器用sass,其他大同小异. 用create-react-app创建项目,执行npm run eject弹出配置文件(此操作不可逆): 配置sass,用的最新的CRA,webpack4 ...

  7. [React] {svg, css module, sass} support in Create React App 2.0

    create-react-app version 2.0 added a lot of new features. One of the new features is added the svgr  ...

  8. CSS Modules 解决 react 项目 css 样式互相影响的问题

    1. CSS Modules引入目的 写过CSS的人,应该都对一大长串选择器选中一个元素不陌生吧,这种方式,其实定义的就是全局样式,我们时常会因为选择器权重问题,没有把我们想要的样式加上去. 另外,每 ...

  9. styled-components:解决react的css无法作为组件私有样式的问题

    react中的css在一个文件中导入,是全局的,对其他组件标签都会有影响. 使用styled-components第三方模块来解决,并且styled-components还可以将标签和样式写到一起,作 ...

随机推荐

  1. kubeadm安装依赖镜像

    使用kubeadm安装的时候如果不能翻墙下载镜像是个很大的问题,这里自己把需要的镜像下载push下留作不时之需 docker pull davygeek/kube-proxy:v1.14.2 dock ...

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

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

  3. ArrayDeque详解

    美人如斯! ArrayDeque是java中对双端队列的线性实现 一.特性 无容量大小限制,容量按需增长: 非线程安全队列,无同步策略,不支持多线程安全访问: 当用作栈时,性能优于Stack,当用于队 ...

  4. 彻底搞懂Javascript的this

    在Javascript中,最玄妙的特性之一,就是this的指向玄幻莫测,一会儿指向这一会儿指向那,让初学者十分伤脑筋. 本文总结一下,方便初学者掌握奥妙之处,同时方便老鸟温故而知新. 首先,看一段代码 ...

  5. Tomcat 配置文件解析工具 Digester

    Digester 是一个依据 xml 配置文件动态构建 Java 对象树的工具,基于 SAX 解析器进行封装,它为 SAX 事件的处理提供了更高级和友好的接口,让开发更专注于要执行的处理,隐藏了 XM ...

  6. JSON数据格式:以及XML文件格式,YML文件格式,properties文件格式

    JSON数据格式:以及XML文件格式,YML文件格式,properties文件格式   数据格式: json数据格式:属于轻量级数据格式,是javascript的一种描述数据的格式.具有易于解析,语法 ...

  7. JAAS configuration for Kafka clients

    Clients may configure JAAS using the client configuration property sasl.jaas.config or using the sta ...

  8. statsvn使用小记

    准备工作 1.安装TortoiseSVN,在安装时需要安装svn命令行工具: 2.拉取svn代码: svn co https://svn.myserver.cn/svn/myproject1\trun ...

  9. vue打包后页面显示空白但是不报错

    在使用vue打包的时候页面显示空白,并且控制台不报错 设置vue-router的时候mode设置为history模式了,这种模式要有后台的配合,一般还是使用哈希模式就不会出现页面空白了.

  10. 32、flex布局

    html: <div class="parent"> <div class="son">1</div> <div cl ...