← Back to React, page 7

react-0123 · Medium mid

How do you decide where to place Suspense boundaries, and how does placement change what users see while data loads?

A Suspense boundary shows its fallback until every suspended component inside it is ready, and then reveals everything inside together. One boundary around a whole page makes users wait for the slowest part. Separate boundaries around independent sections let each part appear as soon as its data arrives. Place boundaries at meaningful UI units, keep layout stable with fallbacks of similar size, and nest them to get progressive reveal without too many spinners.

Full answer

When a component suspends (it reads a promise with use, or loads lazy code), React looks upward for the nearest <Suspense> boundary and shows its fallback in place of that whole boundary's content. When all components inside are ready, they appear together.

Placement decides the experience:

  • One boundary around the page: simple, but the page stays in the fallback until the slowest request finishes. This is a "loading spinner for everything" design.
  • A boundary per independent section (profile, posts, recommendations): the fast sections appear first and the slow one shows its own fallback. Users read content sooner.
  • Nested boundaries: an outer boundary for the layout and an inner one for slow details. React reveals outer content when it is ready, then inner content later.

Guidelines:

  1. Put a boundary where a loading placeholder makes sense visually (a card, a list, a sidebar), not around tiny elements.
  2. Use fallbacks (skeletons) with similar dimensions to the content to prevent layout shift.
  3. Do not create so many boundaries that the page "pops" in dozens of times. Group parts that should appear together.
  4. Fetch in parallel: start all promises before rendering (route loader, parent), because sibling components that suspend one after another in the same boundary create a waterfall.
  5. Use transitions for updates. When already visible content would suspend again, startTransition keeps the old content on screen instead of showing the fallback again.
  6. Pair every boundary with an error boundary decision, because a rejected promise throws to the nearest error boundary.

With streaming server rendering the same boundaries decide what HTML is sent first and what streams later.

The example renders the same two data sources (fast and slow) with one shared boundary and with separate boundaries.

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

const delay = (ms, value) => new Promise((resolve) => setTimeout(() => resolve(value), ms));
const profilePromise = delay(150, 'Profile: Asha');
const postsPromise = delay(300, 'Posts: 2 new');

function Profile() {
  return <p>{use(profilePromise)}</p>;
}

function Posts() {
  return <p>{use(postsPromise)}</p>;
}

export default function Page() {
  return (
    <div>
      <section data-layout="one shared boundary">
        <Suspense fallback={<p>Loading everything...</p>}>
          <Profile />
          <Posts />
        </Suspense>
      </section>
      <section data-layout="separate boundaries">
        <Suspense fallback={<p>Loading profile...</p>}>
          <Profile />
        </Suspense>
        <Suspense fallback={<p>Loading posts...</p>}>
          <Posts />
        </Suspense>
      </section>
    </div>
  );
}

The two promises are created when the module loads, only to keep the demo small. In a real app they would come from a loader or a cache. The check looked at the page at the start, after the fast request, and after the slow one. Output of the run: early => one shared boundary: Loading everything... || separate boundaries: Loading profile...Loading posts... after the fast request => one shared boundary: Loading everything... || separate boundaries: Profile: AshaLoading posts... after the slow request => one shared boundary: Profile: AshaPosts: 2 new || separate boundaries: Profile: AshaPosts: 2 new

Key points

  • A boundary shows its fallback until all suspended parts inside are ready, then reveals them together.
  • One page-wide boundary waits for the slowest data. Separate boundaries reveal sections progressively.
  • Place boundaries at meaningful UI units and size the fallbacks like the content.
  • Start data requests in parallel to avoid waterfalls inside a boundary.
  • Use transitions to avoid hiding visible content again, and add error boundaries.

Common mistakes

  • Wrapping the entire app in one boundary and showing a full-page spinner for every load.
  • Creating the promise inside the component that uses it.
  • Using fallbacks with a different size from the content, causing layout shift.
  • Adding a boundary around every small element.
  • Forgetting an error boundary for rejected promises.

Follow-ups

  • How do transitions prevent a fallback from showing again?
  • How do Suspense boundaries influence streaming server rendering?
  • How would you avoid a waterfall between two sibling components that both fetch?

Tips

  • Describe the "slowest part" problem of a single boundary.
  • Mention fallback size and layout shift.
  • Link boundaries to transitions and error boundaries.