Suspense
React Suspense
Handles asynchronous rendering declaratively. Simplifies loading state management.
React Suspense
Handles asynchronous rendering declaratively. Simplifies loading state management.
React Suspense is a declarative loading-handling mechanism that "suspends" a component's rendering until data is ready and displays a fallback UI (fallback). It allows you to manage asynchronous state cleanly without an if (loading) conditional.
Suspense was originally introduced for code splitting together with React.lazy, but starting with React 18 it was extended so that it can also be used for data fetching. It works in combination with async functions in Server Components or the suspense option of TanStack Query.
Nesting multiple Suspense boundaries lets you manage fine-grained loading states. A parent Suspense waits until all of its children are ready, and each child Suspense handles its loading state independently.
In Streaming SSR, Suspense plays a key role. The server renders up to the Suspense boundary, first sends the HTML including the fallback, and when the data is ready, sends the rest by streaming to fill it in progressively.
import { Suspense, lazy } from 'react';
// 1. ์ฝ๋ ์คํ๋ฆฌํ
๊ณผ Suspense
const HeavyChart = lazy(() => import('./HeavyChart'));
const AdminPanel = lazy(() => import('./AdminPanel'));
function Dashboard() {
return (
<div>
<h1>๋์๋ณด๋</h1>
<Suspense fallback={<ChartSkeleton />}>
<HeavyChart />
</Suspense>
<Suspense fallback={<div>๊ด๋ฆฌ์ ํจ๋ ๋ก๋ฉ์ค...</div>}>
<AdminPanel />
</Suspense>
</div>
);
}
// 2. TanStack Query์ Suspense
import { useSuspenseQuery } from '@tanstack/react-query';
function UserProfile({ userId }: { userId: string }) {
// suspense: true ์ต์
์ด ์๋ ํ์ฑํ
const { data: user } = useSuspenseQuery({
queryKey: ['user', userId],
queryFn: () => fetchUser(userId),
});
// data๋ ํญ์ ์กด์ฌ (undefined ์ฒดํฌ ๋ถํ์)
return <div>{user.name}</div>;
}
function ProfilePage({ userId }: { userId: string }) {
return (
<Suspense fallback={<ProfileSkeleton />}>
<UserProfile userId={userId} />
</Suspense>
);
}
// 3. ์ค์ฒฉ Suspense๋ก ์ธ๋ถํ๋ ๋ก๋ฉ
function ProductPage({ id }: { id: string }) {
return (
<Suspense fallback={<PageSkeleton />}>
<ProductInfo id={id} />
<div className="grid grid-cols-2 gap-4">
<Suspense fallback={<ReviewsSkeleton />}>
<ProductReviews productId={id} />
</Suspense>
<Suspense fallback={<RelatedSkeleton />}>
<RelatedProducts productId={id} />
</Suspense>
</div>
</Suspense>
);
}
// 4. ErrorBoundary์ ํจ๊ป ์ฌ์ฉ
import { ErrorBoundary } from 'react-error-boundary';
function SafeDataFetch() {
return (
<ErrorBoundary fallback={<ErrorMessage />}>
<Suspense fallback={<Loading />}>
<DataComponent />
</Suspense>
</ErrorBoundary>
);
}
// 5. SuspenseList๋ก ๋ก๋ฉ ์์ ์ ์ด (์คํ์ )
import { SuspenseList } from 'react';
function Feed() {
return (
<SuspenseList revealOrder="forwards" tail="collapsed">
<Suspense fallback={<PostSkeleton />}>
<Post id="1" />
</Suspense>
<Suspense fallback={<PostSkeleton />}>
<Post id="2" />
</Suspense>
</SuspenseList>
);
}
In a technical meeting:
"There are so many loading spinners that it's distracting. If we group the Suspense boundaries appropriately, we can handle it cleanly with a single skeleton. Let's discuss the loading units with the UX team."
In a technical interview:
"How does Suspense work?" - "When a child component throws a Promise, Suspense catches it and renders the fallback. When the Promise resolves, it renders the child again."
In a code review:
"If you use useSuspenseQuery, you don't need to check if (isLoading). Instead, you must wrap it in Suspense, and also add an ErrorBoundary to handle errors."
Take a mock interview on this topic โ WIA SOOM AI Interview ยท Start for free