Skip to article content
FrontendXLesson 21 / 40

Suspense Boundaries and Loading States

Build, explain and debug React 19 interfaces: components, rendering, state, effects, identity, forms and actions, server components, concurrency, performance and testing, ending with architecture and typing work on real component challenges.

advanced · about 55 minutes · 4 exercises

  • Predict
  • Explain
  • Implement
  • Debug

What you will be able to do

#
  • Place Suspense boundaries to scope loading to a section.
  • Pair Suspense with an error boundary for failures.
  • Avoid fallback flicker by keeping promises stable.

Overview

#

A boundary is a loading scope

Suspense shows a fallback while its descendants are not ready. A component suspends when it reads a promise with use, or when it is a lazy component that has not loaded. The nearest Suspense boundary above it shows the fallback.

snippet.tsx
<Suspense fallback={<Spinner />}>
  <Comments commentsPromise={commentsPromise} />
</Suspense>

Where you put the boundary matters

The boundary's position decides how much of the page disappears while loading. A boundary at the root blanks the whole page. A boundary around one section keeps the rest of the page interactive. Place boundaries around the parts that load independently.

snippet.tsx
<Layout>
  <Suspense fallback={<HeaderSkeleton />}><Header /></Suspense>
  <Suspense fallback={<FeedSkeleton />}><Feed /></Suspense>
</Layout>

Reveal order

When several siblings suspend, React can reveal them as they resolve. Wrapping them in one boundary makes them appear together; separate boundaries let each appear as soon as it is ready. Choose based on whether a partial reveal looks broken.

Pair with an error boundary

Suspense handles loading, not failure. Put an error boundary outside the Suspense boundary so a rejected promise shows an error UI instead of a fallback forever.

snippet.tsx
<ErrorBoundary fallback={<ErrorCard />}>
  <Suspense fallback={<Spinner />}>
    <Comments commentsPromise={commentsPromise} />
  </Suspense>
</ErrorBoundary>

Pitfall: fallback flicker

A fallback that appears for a few milliseconds looks worse than no fallback. Avoid creating a new promise on every render, which suspends again and again. Cache promises so a component suspends once and then renders.

Pitfall: hiding already-loaded content

When a parent boundary re-suspends, it can hide content that was already visible. Keep boundaries small and avoid suspending on data you already have.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    The promise never resolves. What does the user see?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.tsx
    <Suspense fallback={<Spinner />}>
      <Data dataPromise={p} />
    </Suspense>
    Choose the best answer
  2. Exercise 2Implement

    Wrap two independently loading sections in their own Suspense boundaries.

    starter.tsx
    function Page() {
      return <main><Header /><Feed /></main>;
    }

    Reveal the reference answer
    reference.tsx
    import { Suspense } from 'react';
    
    function Page() {
      return (
        <main>
          <Suspense fallback={<HeaderSkeleton />}><Header /></Suspense>
          <Suspense fallback={<FeedSkeleton />}><Feed /></Suspense>
        </main>
      );
    }

    Authored expected output: Each section shows its own skeleton and appears as soon as it is ready.

    Separate boundaries scope the fallback to each section, so a slow feed does not hide the header and each section reveals independently.

  3. Exercise 3Debug

    This component flashes its fallback on every parent render. Explain and fix.

    starter.tsx
    function Comments() {
      const comments = use(fetch('/api/comments').then((r) => r.json()));
      return <ul>{comments.map((c) => <li key={c.id}>{c.text}</li>)}</ul>;
    }

    Reveal the reference answer
    reference.tsx
    function Comments({ commentsPromise }: { commentsPromise: Promise<Comment[]> }) {
      const comments = use(commentsPromise);
      return <ul>{comments.map((c) => <li key={c.id}>{c.text}</li>)}</ul>;
    }

    Authored expected output: The fallback shows once; the promise is stable across renders.

    Creating the promise inside render produces a new promise every render, so the component suspends again and again. Receiving or caching the promise outside render makes it stable.

  4. Exercise 4Explain

    Explain how the position of a Suspense boundary changes the user experience.

    starter.tsx
    // root boundary vs per-section boundaries

    Reveal the reference answer
    reference.tsx
    The boundary's position sets the loading scope. A boundary at the root unmounts the whole tree and shows one fallback, so the entire page blanks while any descendant loads. Boundaries around individual sections keep the rest of the page visible and interactive, and each section reveals as soon as it is ready. Smaller boundaries give a more granular, less disruptive experience but can introduce more layout shifts, so place them around parts that load and fail independently.

    Authored expected output: A written explanation of loading scope, blanking versus partial reveal, and the layout-shift trade-off.

    The boundary is a loading scope, not just a spinner. Its placement decides what disappears and what stays usable.

Common misconceptions

#
  • Suspense catches errors. It handles loading; pair it with an error boundary for failures.
  • One root boundary is best. A root boundary blanks the whole page; place boundaries around independently loading sections.
  • A component suspends once and stays resolved. Creating a new promise each render suspends again.
  • Suspense only works with lazy components. It also handles components that read promises with use.

Follow-up practice

#
  • react-server-components
  • react-data-fetching-caches

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.