[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; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2017-12-10 [Python] Understand List Comprehensions in Python
2017-12-10 [Python] isinstance() for checking object type
2017-12-10 [Python] Find available methods and help in REPL
2014-12-10 [Node.js] Broswerify -- 2
2014-12-10 [Node.js] Broswerify -- 1
2014-12-10 [MEAN Stack] First API -- 3. Select by ID with Mongoose and Express
2014-12-10 [MEAN Stack] First API -- 1. with Node.js, Express and MongoDB