Lesson 3 of 7
Suspense for Data Fetching
Suspense allows components to 'pause' rendering while waiting for async data, showing a fallback until ready. Combined with TanStack Query's `suspense: true` option or Next.js's RSC streaming, this eliminates loading state management in components entirely.
Suspense with TanStack Query
import { Suspense } from 'react';
import { useSuspenseQuery, useSuspenseInfiniteQuery } from '@tanstack/react-query';
import { ErrorBoundary } from 'react-error-boundary';
// ---- useSuspenseQuery - component suspends until data is ready ----
// NO isLoading check, NO isError check, NO undefined check in the component
function UserProfile({ userId }: { userId: string }) {
const { data: user } = useSuspenseQuery({
queryKey: ['user', userId],
queryFn: () => fetchUser(userId),
// staleTime etc. still work exactly the same
});
// data is ALWAYS defined here - TypeScript knows it too
return <div><h1>{user.name}</h1><p>{user.email}</p></div>;
}
// ---- Parent wraps with Suspense + ErrorBoundary ----
function ProfilePage({ userId }: { userId: string }) {
return (
<ErrorBoundary
fallbackRender={({ error, resetErrorBoundary }) => (
<div>
<p>Error: {error.message}</p>
<button onClick={resetErrorBoundary}>Retry</button>
</div>
)}
>
<Suspense fallback={<UserProfileSkeleton />}>
<UserProfile userId={userId} />
</Suspense>
</ErrorBoundary>
);
}
// ---- Parallel Suspense with useQueries (non-waterfall) ----
function Dashboard() {
// Both queries fire simultaneously even though they're in separate components
return (
<div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 16 }}>
<Suspense fallback={<StatsSkeleton />}>
<Stats /> {/* fetches /api/stats */}
</Suspense>
<Suspense fallback={<NotificationsSkeleton />}>
<Notifications /> {/* fetches /api/notifications */}
</Suspense>
</div>
);
}
// ---- SuspenseList - coordinate multiple Suspense boundaries ----
import { SuspenseList } from 'react'; // experimental
function StaggeredFeed() {
return (
// 'forwards' - shows items top-to-bottom, prevents layout jumps
<SuspenseList revealOrder="forwards" tail="collapsed">
{posts.map(post => (
<Suspense key={post.id} fallback={<PostSkeleton />}>
<Post id={post.id} />
</Suspense>
))}
</SuspenseList>
);
}Tip
Tip
Practice Suspense for Data Fetching in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and flow for Suspense for Data Fetching
Practice Task
Note
Practice Task - (1) Write a working example of Suspense for Data Fetching from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Suspense for Data Fetching is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.
Finished this lesson?