zoukankan      html  css  js  c++  java
  • [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;
    }
  • 相关阅读:
    [bzoj4241] 历史研究 (分块)
    [tyvj2054] 四叶草魔杖 (最小生成树 状压dp)
    20180710 考试记录
    [luogu2047 NOI2007] 社交网络 (floyed最短路)
    [luogu2081 NOI2012] 迷失游乐园 (树形期望dp 基环树)
    [luogu1600 noip2016] 天天爱跑步 (树上差分)
    [luogu2216 HAOI2007] 理想的正方形 (2dST表 or 单调队列)
    [poj 3539] Elevator (同余类bfs)
    [BZOJ1999] 树网的核 [数据加强版] (树的直径)
    bzoj2301 [HAOI2011]Problem b
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12017130.html
Copyright © 2011-2022 走看看