Home / Learn React / Suspense and lazy Loading

Suspense and lazy Loading

<Suspense> shows a fallback (like a spinner) while something below it is not ready: a lazy component, a data promise or a loading route.

In shortWrap slow parts in <Suspense fallback={...}>. Use React.lazy(() => import("./Page")) to load code only when needed.

Theory in plain words

Nested boundariesHeaderPostsSuspense(feed)AdsSuspense(sidebar)Page
Nested boundaries

What it is

Suspense is a boundary. When a component inside it "suspends" (it is waiting), React shows the fallback for that boundary instead. React.lazy loads a component's code on demand.

Why we use it

Without it you write if (loading) return ... in every component. Suspense lets loading states be placed once, near the layout, and it supports code splitting to make the first page load smaller.

How it works

The nearest Suspense above the waiting component takes over and shows its fallback. You can nest boundaries to show parts of the page as soon as they are ready.

Where it is used

  • Code splitting by route or by big component
  • Data fetching with use, TanStack Query or frameworks
  • Streaming server rendering

lazy components

Load a component only when neededTry it Yourself
In a real project
import { lazy, Suspense } from "react";
const Settings = lazy(() => import("./Settings.jsx"));   // separate file, separate JS chunk

<Suspense fallback={<Spinner />}>
  <Settings />
</Suspense>

Two boundaries load independently

Slow part does not block the fast partTry it Yourself
Tip:Keep fallbacks small and similar in size to the real content (a skeleton) so the page does not jump.

Quick quiz

1. What does the fallback prop of Suspense hold?

2. What does React.lazy help with?

3. Two separate Suspense boundaries...

Common questions

What does the fallback prop of Suspense hold?

What to show while waiting. For example a spinner or a skeleton.

What does React.lazy help with?

Loading a component code only when needed. It enables code splitting.