React Suspense
<Suspense> lets a component wait for something (code or data) and tells React what to show in the meantime. You wrap part of the tree in a boundary with a fallback. If anything inside isn’t ready, React renders the fallback for that boundary and swaps in the real content when it is.
1. The basic case: lazy-loaded code
import { lazy, Suspense } from 'react';
const Settings = lazy(() => import('./Settings'));
function App() {
return (
<Suspense fallback={<Spinner />}>
<Settings />
</Suspense>
);
}
The Settings bundle downloads the first time it renders. Until then the user sees <Spinner />. See code splitting and lazy loading.
2. What can suspend
React.lazycomponents.- Reading a promise with
use(React 19). - Data libraries and frameworks with Suspense support.
import { use, Suspense } from 'react';
function Comments({ commentsPromise }) {
const comments = use(commentsPromise); // suspends until it resolves
return comments.map((c) => <p key={c.id}>{c.text}</p>);
}
function Post({ commentsPromise }) {
return (
<Suspense fallback={<p>Loading comments...</p>}>
<Comments commentsPromise={commentsPromise} />
</Suspense>
);
}
What does not suspend: a fetch inside useEffect. Suspense can’t see it, so you still need your own loading state there.
3. Boundaries decide what the user sees
Where you put boundaries is a design decision.
<Suspense fallback={<PageSkeleton />}>
<Header />
<Suspense fallback={<FeedSkeleton />}>
<Feed />
</Suspense>
<Suspense fallback={<SidebarSkeleton />}>
<Sidebar />
</Suspense>
</Suspense>
- One big boundary: everything appears together, after the slowest part.
- Several small ones: parts appear as they’re ready, at the cost of more layout movement.
Group things that should appear together under one boundary.
4. Transitions keep existing content on screen
If already-visible content suspends again (say, after a tab change), React would replace it with the fallback. Mark the update as a transition and React keeps the old UI until the new one is ready:
const [isPending, startTransition] = useTransition();
function selectTab(next) {
startTransition(() => setTab(next));
}
Use isPending to dim the old content. useDeferredValue does the same for a value, which suits search results.
5. Suspense on the server
With streaming server rendering, React sends the page shell and each boundary’s fallback right away, then streams each section’s HTML as it finishes. Boundaries also hydrate independently, and React hydrates the part the user interacts with first. That means one slow query no longer delays the whole page. More in SSR vs CSR.
6. Errors need a different boundary
Suspense handles “not ready yet”. A rejected promise or a failed chunk is an error, and that goes to the nearest error boundary. In practice you wrap both:
<ErrorBoundary fallback={<RetryPanel />}>
<Suspense fallback={<Spinner />}>
<Feed />
</Suspense>
</ErrorBoundary>
7. Pitfalls
- Creating the promise during render.
use(fetch(url))creates a new promise on every render, so the component keeps suspending. Create the promise outside the component, in a loader, or through a cache. - Waterfalls. A parent that suspends hides its children, so their requests don’t start until it finishes. Start requests in parallel, as high as you can.
- Fallback flashes. A spinner that shows for 80 ms looks worse than nothing. Use transitions, or skeletons that match the final layout.
Choosing a loading strategy
| Situation | Approach |
|---|---|
| Route or heavy component loaded on demand | lazy + <Suspense> |
| Data from a Suspense-enabled framework or library | <Suspense> boundaries per section |
| Update that would hide visible content | useTransition / useDeferredValue |
Fetch inside useEffect |
Manual loading state |
| Failure | Error boundary around the Suspense boundary |
Follow-up questions to expect
- “How does a component ‘suspend’?” It throws a promise during render. React catches it at the nearest boundary, shows the fallback, and re-renders when the promise settles.
- “Suspense vs a loading flag?” Suspense moves the loading UI out of every component and into the tree structure, and lets React coordinate what appears together.
- “What happens to state inside a suspended tree?” On first mount nothing was committed, so there’s no state to keep. Already-mounted content that re-suspends keeps its state while hidden.
