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. 注解@Slf4j的使用

    注解@Slf4j的使用 声明:如果不想每次都写private  final Logger logger = LoggerFactory.getLogger(当前类名.class); 可以用注解@Slf ...

  2. MySQL面试题及答案整理,史上最全!

    原文链接:https://juejin.im/post/5d351303f265da1bd30596f9 前言 本文主要受众为开发人员,所以不涉及到MySQL的服务部署等操作,且内容较多,大家准备好耐 ...

  3. 使用位运算实现int32位 整数的加减乘除

    我觉得比较难想的是加法吧. 首先加法,脑海中脑补二进制加法,相同位相加,超过2 ,则进1,留0 那么用位运算怎么实现呢?其实理解了异或和与操作,就很容易想出来了. 我觉得异或操作和与操作完全就是实现加 ...

  4. K8S学习笔记之K8S日志搜集实战

    详细参考这篇文章,几乎覆盖了了K8S的各种日志搜集方案 https://juejin.im/post/5b6eaef96fb9a04fa25a0d37#heading-8

  5. asp.net图片浏览器效果

    技术来源于同学会实践 前台设计 <%@ Page Language="C#" AutoEventWireup="true" CodeFile=" ...

  6. Golang逃逸分析

    Golang逃逸分析 介绍逃逸分析的概念,go怎么开启逃逸分析的log. 以下资料来自互联网,有错误之处,请一定告之. sheepbao 2017.06.10 什么是逃逸分析 wiki上的定义 In ...

  7. [Windows] - Windows/Office纯绿色一键激活工具及方法

    瘟到死网上有很多一件键激活工具(如KMS),但许多带毒或报毒.这里给出一个纯绿色命令行一键激活,及自已搭建激活服务器的方法. KMS现在算法都是公开的了,可以自行在网上找到,这里不详述. 使用命令行一 ...

  8. 1.ZooKeeper ACL权限控制

    参考:https://blog.csdn.net/liuxiao723846/article/details/79391650 ZK 类似文件系统,Client 可以在上面创建节点.更新节点.删除节点 ...

  9. Java打印日历表

    今天来吐槽一下Java的Calendar类的使用问题,反正我是弄了半天. 首先是,遇到一个问题,输入年份和月份,需要打印这个月的日历,网上有不少代码,但我用了几个感觉都不是很靠谱. 然后经过一番探索, ...

  10. node.js中的fs.rename()方法

    node.js 中的fs.rename()模块 var fs=require('fs');//node.js的核心模块 原生模块.修改文件名称,可更改文件的存放路径 方法说明 : 修改文件名称,可更改 ...