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. C语言交换两个指针所指位置的数值

    交换指针变量x和y所指向的存储位置处存放的值,不需要第三个位置来存储临时变量.这种方式并没有性能上的优势. void replace(int *x, int *y) { *y = *x ^ *y; * ...

  2. python docker api

    开启Remote API docker默认是没有开启Remote API的,需要我们手动开启.编辑/lib/systemd/system/docker.service文件, 在文件里的ExecStar ...

  3. mybatis-plus 主键自增问题

    主键不自增:返回值是插入的条数 <insert id="add" parameterType="EStudent"> insert into TSt ...

  4. JqGrid参考实例

    <div class="gridtable mt5"> <table id="tbList"></table> <di ...

  5. tomcat端口修改后在Eclipse中启动无效问题解决

    原文:https://blog.csdn.net/yangyiqian/article/details/40262039 问题:在conf目录修改了server.xml配置中的节点  <Conn ...

  6. vue中使用radio和checkbox

    代码 <template> <div id="app"> <input type="checkbox" v-model=" ...

  7. Linux下搭建keepalive+nginx

    一. 安装nginx(略) 二. 安装keepalive 下载http://www.keepalived.org/download.html 安装依赖包 yum install –y popt* gc ...

  8. 2019 哔哩哔哩java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.哔哩哔哩等公司offer,岗位是Java后端开发,因为发展原因最终选择去了哔哩哔哩,入职一年时间了,也成为了面 ...

  9. robotframework-ride1.7.3.1更新安装

    在2019年之前,robotframework-ride的版本一直是1.5.2.1,是2016年1月份的版本,里面需要使用 wxPython2.8-win64-unicode-2.8.12.1-py2 ...

  10. React学习笔记②

    import React,{Component} from 'react'; import Child from './Child.js' class App extends Component{ c ...