[React] Use CSS Transitions to Avoid a Flash of Loading State
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的更多相关文章
- CSS transitions深入理解
到底css transition是什么,我们来看w3c的解释: CSS Transitions allow property changes in CSS values to occur smooth ...
- CSS: transitions
CSS Transitions CSS transitions allows you to change property values smoothly (from one value to ano ...
- React中css的使用
网页的布局.颜色.形状等UI展示方式主要是由Css进行设置,在ReactJs中也是一样.ReactJs中的Css结构方式与传统的Web网页类似,但依然存在一些差异.ReactJs中Css文件本身的编写 ...
- React Transition css动画案例解析
实现React Transition Css动画效果 首先在项目工程中引入react-transition-group: npm install react-transition-group --sa ...
- Flash网站Loading制作
Flash网站Loading制作~~~ stop(); stage.scaleMode=StageScaleMode.NO_SCALE; //指定舞台属性为不跟随播放器大小而改变 stage.show ...
- create-react-app创建react项目 css模块化处理
用的css预处理器用sass,其他大同小异. 用create-react-app创建项目,执行npm run eject弹出配置文件(此操作不可逆): 配置sass,用的最新的CRA,webpack4 ...
- [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 ...
- CSS Modules 解决 react 项目 css 样式互相影响的问题
1. CSS Modules引入目的 写过CSS的人,应该都对一大长串选择器选中一个元素不陌生吧,这种方式,其实定义的就是全局样式,我们时常会因为选择器权重问题,没有把我们想要的样式加上去. 另外,每 ...
- styled-components:解决react的css无法作为组件私有样式的问题
react中的css在一个文件中导入,是全局的,对其他组件标签都会有影响. 使用styled-components第三方模块来解决,并且styled-components还可以将标签和样式写到一起,作 ...
随机推荐
- Sitecore客户体验成熟度模型之旅
“成熟”这个词带来了很多想法:你在青年时不愿意找到工作?你四岁的孩子偶尔发脾气?可能还有你的公司能否在数字化时代提供个性化的客户体验? 你如何定义CX成熟度?如果您的CX战略仍处于开发阶段,您需要达到 ...
- VS2013中反汇编常用指令理解
最近复习C语言,对反汇编感兴趣,就用下图举例解释一下我的理解,如有错还请大佬指教. 首先,认识两个常用指令 : lea ---> 取地址赋值 mov ---> (同类型)赋值 ...
- SpringBootSecurity学习(18)前后端分离版之 OAuth2.0 数据库(MyBatis)存储客户端
使用Mybatis查询客户端信息 前面的例子使用了默认的jdbc配置来动态从数据库查询客户端信息,下面来改用更加灵活的mybatis来实现,改用mybatis,首先pom中换成mybatis的依赖: ...
- vs2017专业版和企业版的密钥
Enterprise: NJVYC-BMHX2-G77MM-4XJMR-6Q8QF Professional: KBJFW-NXHK6-W4WJM-CRMQB-G3CDH
- MVC伪静态路由简单搭配
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute ...
- 【转】Unobtrusive Ajax的使用
[转]Unobtrusive Ajax的使用 Ajax (Asynchronous JavaScript and XML 的缩写),如我们所见,这个概念的重点已经不再是XML部分,而是 Asynchr ...
- Git 理解修改
参考链接:https://www.liaoxuefeng.com/wiki/896043488029600/897884457270432 Git之所以比其他版本控制系统设计得优秀,就是因为Git跟踪 ...
- 第四周(1):数据分布-Python实战
数据准备 数据集地址:http://jse.amstat.org/datasets/normtemp.dat.txt 数据集描述:总共只有三列:体温.性别.心率 数据集详细描述:Journal of ...
- IDEA超级实用的几个快捷键
最近开始使用IDEA,突然发现的比较的实用的几个快捷键 这些快捷键用的好的话真的可以提升很多效率 还有一些比较简单的快捷键,相信大家都会 Ctrl+X:剪切 Ctrl+C:复制 Ctrl+V:粘贴 C ...
- [译] Ruby如何访问Excel文件
Parsing Excel Files with Ruby BY: MATT NEDRICH 翻译:佣工7001 本文中,我将会评判几种Ruby语言访问Excel文件的库.我将要讨论针对不同格式 ...