React suspense/lazy delay?

后端 未结 5 1668
醉话见心
醉话见心 2020-12-05 18:13

I am trying to use the new React Lazy and Suspense to create a fallback loading component. This works great, but the fallback is showing only a few ms. Is there a way to add

5条回答
  •  遥遥无期
    2020-12-05 18:37

    As mentioned by loopmode, component fallback should have a timeout.

    import React, { useState, useEffect } from 'react'
    
    const DelayedFallback = () => {
      const [show, setShow] = useState(false)
      useEffect(() => {
        let timeout = setTimeout(() => setShow(true), 300)
        return () => {
          clearTimeout(timeout)
        }
      }, [])
    
      return (
        <>
          {show && 

    Loading ...

    } ) } export default DelayedFallback

    Then just import that component and use it as fallback.

    }>
           
    
    

提交回复
热议问题