Course contentsShow
← React
Lesson 1338 of 2,39634. Suspense FundamentalsFree lesson

The Suspense Component API

How to wrap components in <Suspense> and provide a fallback prop that renders while content is loading.

The Suspense Component API

You'll learn how to use React's <Suspense> component to declaratively handle loading states while asynchronous content resolves.

The Core Idea

The <Suspense> component wraps parts of your component tree that might "suspend" (pause rendering while waiting for data). Instead of manually tracking loading states with flags and conditionals, you wrap components in <Suspense> and provide a fallback prop—React automatically displays the fallback UI while content loads, then swaps in the real content when ready.

Think of it like a placeholder system: you tell React "if anything inside this boundary isn't ready yet, show this loading UI instead."

Basic API

The <Suspense> component accepts two key props:

  • fallback: A React element to display during loading (spinners, skeletons, "Loading..." text)
  • children: The components that might suspend
import { Suspense } from 'react';

<Suspense fallback={<div>Loading...</div>}>
  <ComponentThatMightSuspend />
</Suspense>

When ComponentThatMightSuspend starts loading data, React automatically shows the fallback. Once data arrives, React replaces the fallback with the actual component.

Multiple Suspense Boundaries

You can nest and place multiple <Suspense> boundaries throughout your tree:

<Suspense fallback={<PageLoader />}>
  <Header />
  <Suspense fallback={<Spinner />}>
    <UserProfile />
  </Suspense>
  <Footer />
</Suspense>

The inner boundary catches suspensions from <UserProfile>, while the outer boundary catches everything else. This creates granular loading states without prop drilling or complex state management.

Key Takeaway: The <Suspense> component wraps potentially-suspending components and automatically renders its fallback prop during loading, providing a declarative way to handle asynchronous UI states without manual loading flags.