Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 39 ยท Resources, Suspense and Async

Suspense and Transitions

Show loading states declaratively with Suspense, order reveals with SuspenseList and keep old content on screen during updates with useTransition in SolidJS.

In the last lesson you handled loading by hand: Show when={user()} with a fallback. That works for one resource, but it gets repetitive when a screen reads several. Suspense lets you say once, "while anything inside here is loading, show this instead."

You will learn how Suspense detects loading, how to nest boundaries, how SuspenseList coordinates several of them, and how transitions keep the old screen visible while new data loads so the page does not flash back to a spinner.

How Suspense works

<Suspense fallback={...}> wraps a part of the tree. When any resource that is read inside that subtree is loading, Solid shows the fallback; when all of them are ready, it shows the children.

JSX
<Suspense fallback={<p>Loading...</p>}>
  <Profile />   {/* reads user() */}
  <Posts />     {/* reads posts() */}
</Suspense>

The key word is read. Suspense does not look at which resources exist, only at which ones a component reads during rendering. Creating a resource without reading its value does not suspend anything.

Note the difference from React: in Solid the children are created immediately and their DOM is kept alive while the fallback shows. Suspense only hides and reveals; it does not re-run your components.

One Suspense, two resources

The page shows one fallback until the slower resource (1.5 seconds) finishes, and then both lines appear together. Next you will see how to avoid waiting for the slowest one.

Nested boundaries

Place a boundary closer to the slow part, and the rest of the page appears sooner. The nearest Suspense above the reading component catches the loading state.

JSX
<Suspense fallback={<PageSkeleton />}>
  <Header />                     {/* needs user() */}
  <Suspense fallback={<p>Loading feed...</p>}>
    <Feed />                     {/* needs posts() */}
  </Suspense>
</Suspense>

Now the header appears as soon as the user is ready and the feed has its own placeholder.

flowchart TD
  A["Outer Suspense: page"] --> B["Header (user)"]
  A --> C["Inner Suspense: feed"]
  C --> D["Feed (posts)"]

A component suspends the nearest boundary above it. Inner boundaries stop the outer one from waiting.

SuspenseList

When several boundaries sit side by side, they may finish in random order and the page jumps around. SuspenseList coordinates them:

JSX
<SuspenseList revealOrder="forwards" tail="collapsed">
  <Suspense fallback={<Card.Skeleton />}><Card id={1} /></Suspense>
  <Suspense fallback={<Card.Skeleton />}><Card id={2} /></Suspense>
  <Suspense fallback={<Card.Skeleton />}><Card id={3} /></Suspense>
</SuspenseList>
  • revealOrder="forwards" reveals items top to bottom, even if item 3 is ready first.
  • "backwards" reveals from the bottom; "together" reveals all at once.
  • tail="collapsed" shows only the next pending fallback, "hidden" shows none.

SuspenseList is still marked experimental, so check the docs before relying on it in production.

The flash problem and transitions

Suspense has one annoying behavior. Suppose the user clicks "page 2" and the page reads a resource that depends on the page number. The resource starts loading, so the nearest Suspense swaps the already visible content for the fallback. A fast click path flickers: content, spinner, content.

A transition fixes this. While a transition is pending, Solid keeps showing the old content, runs the update in the background, and switches when everything is ready. You use it with useTransition:

JavaScript
const [pending, start] = useTransition();

start(() => setPage(2));   // old page stays visible until page 2 is ready
// pending() is true in the meantime, use it for a subtle indicator
Transition keeps the old page

Press the transition button: the old text stays and "Updating..." appears. Press the plain button: the content disappears and the fallback shows. The first behavior is usually what users prefer for navigation and tabs.

There is also a standalone startTransition function (the same thing without the pending signal). Router navigation in SolidStart and Solid Router already uses transitions, which is why page changes there do not flash.

When to use which

  • Suspense alone for initial loads, where there is nothing to show yet.
  • Suspense + transition for updates, where something sensible is already on screen.
  • resource.latest or refreshing state for tiny updates where a boundary is overkill.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What makes a Suspense boundary show its fallback?

    Suspense reacts to reads of loading resources, not to the mere existence of a resource.
  2. 2. How do you let the header appear before a slow feed is ready?

    A component suspends the nearest boundary above it, so an inner boundary shields the outer content.
  3. 3. What does a transition do while new data loads?

    The update runs in the background and is committed together once ready, avoiding a flash of fallback.
  4. 4. What does revealOrder="forwards" do in SuspenseList?

    It keeps the page stable by revealing items in order even when a later one is ready earlier.

Summary

  • Suspense shows a fallback while resources read inside it are loading; its children are still created.
  • Nest boundaries so fast parts do not wait for slow parts.
  • SuspenseList orders reveals (forwards, backwards, together) and is experimental.
  • useTransition/startTransition keep old content on screen during updates and expose a pending signal.

Next: Error Boundaries and Lazy Loading