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. ubuntu 18 docker 搭建Prometheus+Grafana

    Prometheus(普罗米修斯)是一套开源的监控&报警&时间序列数据库的组合,起始是由SoundCloud公司开发的.随着发展,越来越多公司和组织接受采用Prometheus,社会也 ...

  2. Java学习:线程实现方式

    线程实现方式 并发与并行 并发:指两或多个事件在同一个时间段内发生 并行:指两或多个事件在同一个时刻发生(同时发生) 进程的概念 内存:所有的应用程序都需要进入到内存中执行 临时存储RAM 硬盘:永久 ...

  3. oc的运行时系统

    Objective-C is a class-based object system. Each object is an instance of some class; the object's i ...

  4. HTML中的元素是有属性的:标准与解释器

    元素的属性只有有标准和相应的解释器才有存在的意义. HTML中的元素是有属性的:这些额外的属性值可以配置元素或者以各种方式来调整元素的行为,进而满足用户所需的标准. https://developer ...

  5. 《即时消息技术剖析与实战》学习笔记3——IM系统如何保证消息的实时性

    IM 技术经历过几次迭代升级,如图所示: 从简单.低效的短轮询逐步升级到相对效率可控的长轮询: 全双工的 Websocket 彻底解决了服务端的推送问题: 基于 TCP 长连接衍生的 IM 协议,能够 ...

  6. spring注解@postConstruct与constructor与@Autowired的启动顺序

    本文链接:https://blog.csdn.net/zpflwy1314/article/details/80797756 @Postcontruct’在依赖注入完成后自动调用,例如要将对象a注入到 ...

  7. 对ssm框架里面的一些常用注解的理解

    @Componcnt :作用就是把当前类对象存入spring容器中 属性:value 用于指定bean的id 当我们不写的时候默认就是当前类名,并且首字母要小写 ------------------- ...

  8. 三分钟掌握,使用Quqrtz.Net实现定时发送邮件

    在实际的项目中,常遇到延时触发工作以及定时触发工作 这里所讲的是借助第三方的组件 Quartz.Net 来实现(源码位置:https://github.com/quartznet/quartznet) ...

  9. VUE组件3 数据流和.sync修饰符

    单向数据流:数据通过prop从父组件传递到子组件中,当父级组件中的数据更新时,传子组件也会更新,但不能在子组件中修改.防止子组件在无意中修改,改变父级组件状态 然而,双向数据绑定在某些情况下有用.如果 ...

  10. consul:架构

    官方文档:https://www.consul.io/docs/internals/architecture.html