-
A common UX is to not show loading state too early but instead after a short pause. One possible implementation I could come up with to keep it nicely in a hook is this: function useLoading(loading, delay = 500) {
const [isLoading, setIsLoading] = useState(loading)
useEffect(() => {
return setTimeout(() => setIsLoading(true), delay)
}, [loading]
return isLoading
} and this could then be re-used inside other hooks: function useOrder() {
const { data: order, error, loading } = useSWR("/order", get)
const loading = useLoading(!order && !error, 500)
return {
order,
loading,
error,
}
} Does this make sense? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
That makes sense, I usually use React.Suspense to handle it the loading and try to prefetch the data as soon as possible to reduce the loading time, and using Suspense in the future with Concurrent Mode you will be able to use useTransition to define this delay while fetching data. |
Beta Was this translation helpful? Give feedback.
That makes sense, I usually use React.Suspense to handle it the loading and try to prefetch the data as soon as possible to reduce the loading time, and using Suspense in the future with Concurrent Mode you will be able to use useTransition to define this delay while fetching data.