Suspense and use

Topic 2 of 4 in Data fetching7 min readIntermediateFree course

Overview

Loading states are usually scattered through components: if (isLoading) return <Spinner /> in one place, a skeleton in another. Suspense lets you declare loading states around parts of the tree instead. A component that isn’t ready “suspends”, and React shows the nearest boundary’s fallback until it is.

Suspense boundaries

JSX
import { Suspense } from 'react';

<Suspense fallback={<Spinner />}>
  <CourseHeader />
  <Lessons />
</Suspense>

If any component inside the boundary suspends, React shows the fallback in place of the whole boundary’s content. When everything inside is ready, React reveals it all at once — no half-loaded layouts popping in piece by piece.

A Suspense boundary with a Spinner fallback wraps CourseHeader and Lessons, where Lessons reads a promise with use. While Lessons is waiting, the boundary shows the spinner. Once the data arrives, it shows CourseHeader and the list of lessons. If any child suspends, the nearest boundary shows its fallback, then reveals everything together.

What can suspend?

  • components that read a promise with use (this lesson),
  • components loaded with lazy() while their code downloads (see Code splitting and Activity),
  • data libraries’ Suspense modes (TanStack Query’s useSuspenseQuery) and framework data fetching (Server Components).

Note that a plain useEffect + useState fetch does not suspend — Suspense only works with data sources that integrate with it.

Reading promises with use

React 19’s use API reads the value of a promise:

JSX
import { use } from 'react';

function CourseHeader({ coursePromise }) {
  const course = use(coursePromise);   // suspends until the promise resolves
  return <h1>{course.title}</h1>;
}

While the promise is pending, CourseHeader suspends and the nearest <Suspense> shows its fallback. When it resolves, React renders CourseHeader again and use returns the value. No loading state, no effect, no useState.

Where do the promises come from?

Don’t create promises during render:

JSX
function CourseHeader({ id }) {
  const course = use(fetch(`/api/courses/${id}`).then((r) => r.json())); // ✗
}

Every render creates a new promise, so the component suspends, re-renders, creates another promise, suspends again… forever. React warns: “A component was suspended by an uncached promise. Creating promises inside a Client Component or hook is not yet supported…”

The promise must be stable across renders. Create it:

  • in a parent and pass it down as a prop,
  • in an event handler or a router loader,
  • in a cache keyed by id,
  • on the server, in a Server Component, passing it to a Client Component.
JSX
// A tiny cache: one promise per URL
const cache = new Map();

export function fetchJson(url) {
  if (!cache.has(url)) {
    cache.set(url, fetch(url).then((r) => {
      if (!r.ok) throw new Error(`Request failed: ${r.status}`);
      return r.json();
    }));
  }
  return cache.get(url);
}

function CourseHeader({ id }) {
  const course = use(fetchJson(`/api/courses/${id}`));   // ✓ same promise each render
  return <h1>{course.title}</h1>;
}

Hand-rolled caches like this don’t handle invalidation, errors and memory well — in real apps, let TanStack Query or your framework own the cache.

Starting work early

A big benefit of passing promises down: you can start fetching before the component that needs the data renders, avoiding waterfalls:

JSX
function CoursePage({ id }) {
  // both requests start immediately, in parallel (fetchJson caches each promise by URL)
  const coursePromise = fetchJson(`/api/courses/${id}`);
  const reviewsPromise = fetchJson(`/api/courses/${id}/reviews`);

  return (
    <Suspense fallback={<PageSkeleton />}>
      <CourseHeader coursePromise={coursePromise} />
      <Suspense fallback={<p>Loading reviews…</p>}>
        <Reviews reviewsPromise={reviewsPromise} />
      </Suspense>
    </Suspense>
  );
}

Designing loading sequences

Boundaries are a design tool. Ask: which parts should appear together, and which can arrive later?

  • One boundary around the whole page: nothing appears until everything is ready — simple, but slow data blocks fast data.
  • Nested boundaries: the course header appears first, reviews stream in later. Each boundary is a point where the page can be revealed.
  • Too many boundaries: spinners everywhere, content jumping around.

A good default is one boundary per meaningful section, with fallbacks that match the layout of the real content (skeletons), so nothing shifts when data arrives.

Avoiding hidden content on updates

If an already-visible component suspends again (say, the user picks a different course), React would normally swap the content back to the fallback. When the update is wrapped in a transition, React instead keeps showing the old content until the new content is ready:

JSX
const [isPending, startTransition] = useTransition();

function selectCourse(id) {
  startTransition(() => setCourseId(id));
}

<div style={{ opacity: isPending ? 0.6 : 1 }}>…</div>

Routers do this automatically for navigations. You’ll learn more in Transitions and deferred values.

Errors

If a promise rejects, use throws the error, and it propagates to the nearest error boundary — the “error” equivalent of a Suspense boundary (see Error boundaries):

JSX
<ErrorBoundary fallback={<p>Couldn’t load reviews.</p>}>
  <Suspense fallback={<p>Loading reviews…</p>}>
    <Reviews reviewsPromise={reviewsPromise} />
  </Suspense>
</ErrorBoundary>

Note that use can’t be wrapped in try/catch — React’s lint rules flag it. Handle errors with boundaries, or .catch() on the promise to provide a fallback value.

use is flexible

Unlike other hooks, use may be called inside conditions and loops:

JSX
function Price({ pricePromise, isFree }) {
  if (isFree) return <span>Free</span>;
  const price = use(pricePromise);
  return <span>${price}</span>;
}

It can read context too — use(ThemeContext) — as you saw in Context.

The big picture: Suspense separates what a component needs from how the page handles waiting. Components just read data; boundaries decide what users see in the meantime.

Exercise

Load a course and its reviews

A parent creates two promises once: coursePromise and reviewsPromise (passed in as props — don’t create promises during render). Write CourseHeader({ coursePromise }) and Reviews({ reviewsPromise }) that read them with use. Render the header inside a Suspense boundary with a “Loading course…” fallback, and reviews in a nested boundary with “Loading reviews…”, so the header can appear before the reviews. Show “No reviews yet” for an empty list.

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

export function CoursePage({ coursePromise, reviewsPromise }) {
  // your code here
}

Summary

  • <Suspense fallback={…}> shows the fallback while any component inside it is waiting for something.
  • use(promise) reads a promise’s value; the component suspends until it resolves.
  • Don’t create a promise during render — create it in a loader, event handler, cache or parent and pass it down.
  • Nested Suspense boundaries reveal content in stages; place them where a loading state makes sense to users.
  • Rejected promises throw to the nearest error boundary; use can also read context and is allowed in conditions.