javascript / intermediate
Snippet
Streaming UI with React Suspense
Streaming allows you to break down the page's HTML into smaller chunks and progressively send those chunks from the server to the client. This prevents slow data fetches from blocking the entire page render.
snippet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { Suspense } from 'react';import { Skeleton } from './ui/skeleton';import SlowComponent from './components/SlowComponent';export default function Page() {return (<section><h1>Dashboard</h1><Suspense fallback={<Skeleton />}><SlowComponent /></Suspense></section>);}
nextjs
Breakdown
1
import { Suspense } from 'react';
Imports the Suspense component to handle asynchronous component loading boundaries.
2
<Suspense fallback={<Skeleton />}>
Defines a loading state (the Skeleton) that displays while the child component is resolving.