← Back to React, page 1

react-0018 · Medium mid

What is Suspense in React, how does it work, and what can trigger it?

Suspense lets a component wait for something, such as lazy-loaded code or data, and shows a fallback until it is ready. A component "suspends" when it throws a pending promise, which React handles for you. The nearest Suspense boundary above shows its fallback. Triggers are React.lazy, the use API with a promise, and data-fetching libraries or frameworks that integrate with Suspense, not a plain useEffect fetch.

Full answer

Without Suspense, every component that loads data manages its own loading flag. With Suspense, the component simply reads the data. If the data is not ready, React pauses rendering that part of the tree and shows the closest <Suspense fallback={...}> above it. When the data arrives, React tries again and shows the real content.

Internally the pending promise is treated as a signal. React catches it, renders the fallback, and re-renders when the promise settles. You never see this in your own code.

The sources that work with Suspense are:

  • React.lazy for code splitting.
  • The use API in React 19, which reads a promise (or a context) during render. The promise must be created outside render or come from a cache, not be created fresh in every render, otherwise it never settles in the way the component expects.
  • Libraries and frameworks that support it, such as TanStack Query's suspense mode, Relay and the Next.js App Router.

A fetch inside useEffect does not trigger Suspense, because React cannot see that a component is waiting.

Where you put boundaries is a design choice. One boundary around a page shows one spinner. Several small boundaries let parts appear as they are ready and avoid a blank page. Nested boundaries show the nearest fallback only.

Suspense works with transitions. When a state change in startTransition makes something suspend, React keeps the old UI on screen instead of replacing it with the fallback. On the server, Suspense enables streaming: the server sends the page shell first and streams the slower parts as they resolve.

Errors from a rejected promise are thrown to the nearest error boundary, so pair the two.

jsx
import { Suspense, use } from 'react';

function loadMarks() {
  return new Promise((resolve) => {
    setTimeout(() => resolve([82, 47, 91]), 20);
  });
}

const marksPromise = loadMarks(); // created once, outside render

function Average() {
  const marks = use(marksPromise);
  const average = marks.reduce((sum, m) => sum + m, 0) / marks.length;
  return <p>Average: {average.toFixed(1)}</p>;
}

export default function App() {
  return (
    <Suspense fallback={<p>Loading marks...</p>}>
      <Average />
    </Suspense>
  );
}

Average calls use with a promise that is created once, outside the component. While it is pending, the fallback is shown. First render, while the promise is pending: <p>Loading marks...</p> After the data arrives: <p>Average: 73.3</p>

Key points

  • A Suspense boundary shows a fallback while a child waits for code or data.
  • React.lazy, the use API and Suspense-aware data libraries trigger it. A useEffect fetch does not.
  • Create the promise outside render or in a cache. Do not make a new promise on each render.
  • Several boundaries give finer loading states than one.
  • Suspense plus transitions keeps old content, and Suspense plus server rendering enables streaming.

Common mistakes

  • Creating the promise inside the component so a new promise is made on every render.
  • Expecting Suspense to detect a useEffect fetch.
  • Wrapping the whole app in one boundary and showing a full-page spinner for small waits.
  • Forgetting an error boundary for rejected promises.

Follow-ups

  • How does Suspense work with startTransition to avoid showing a fallback on navigation?
  • What does streaming server rendering gain from Suspense boundaries?
  • Why is use different from other hooks regarding conditionals?

Tips

  • Explain that the component "waits" and the boundary "decides what to show". It ties the pieces together.
  • State the promise-in-render trap. It is a favourite interview catch.
  • Mention error boundaries as the pair for rejection.