React Suspense Simplified

Pro Members Only

This lesson is available if you have an active subscription.

Alternatively, some member might be able to gift it to you.

If you already have a subscription, you can sign in.

React Suspense Simplified

Subscription Required

You must have an active subscription to access this content.
If you already have a subscription, you can sign in.

Suspense Simply Explained

Suspense can be a daunting concept for a lot of React developers. But it doesn’t have to be so lets simplify that.

At its core, Suspense is nothing more than a React built in component, that allows you show a pending state when some child component is taking a while to render.

Suspense is a core React component:

import { Suspense } from 'react';

Demo

In the below example the PrimeFactors takes a while to render as its waiting for an api call to complete. This will actually prevent the server from returning the page.

To fix this we wrap the offending PrimeFactors component in Suspense with fallback set to a PrimeFactorsLoading component. This way the heading is returned immediately and while the PrimeFactors is still busy completing its render on the server, the PrimeFactorsLoading component is rendered. Eventually once PrimeFactors is done rendering, Suspense seamlessly replaces PrimeFactorsLoading with PrimeFactors.

app/page.tsx
ui/PrimeFactors.tsx
ui/PrimeFactorsLoading.tsx
import { PrimeFactors } from "@/ui/PrimeFactors";
import { PrimeFactorLoading } from "@/ui/PrimeFactorsLoading";
import { Suspense } from "react";

export default function Home() {
const input = 6999999989 * 7000000001;
return (
<>
<h1 className="text-3xl">Prime Factors for {input}</h1>
<Suspense fallback={<PrimeFactorLoading />}>
<PrimeFactors input={input} />
</Suspense>
</>
);
}
javascript
typescript
react
playwright

Enjoy free content straight from your inbox 💌

No spam, unsubscribe at any time.

Transcript

00:00

Suspense can be a daunting concept for a lot of React developers, but it doesn't have to be. So let's simplify it. Edit score suspense is nothing more than a react built-in component that allows you to show a pending state while some child component is taking a while to render. As an example, consider a simple server component that cannot render because it is waiting on some API request to complete. So instead of leaving the user hanging with the blank screen, we can wrap our component with React suspense. This allows us to provide a fallback UI so that the user is notified that something is happening,

00:33

and once the render is complete, suspense will automatically swap out the fallback UI with the final component. With the theory out of the way, let's take a look at some practical code. Within a simple page, we wish to compute the prime factors of a given input. For this purpose, we will build a component called prime factors. That takes the input and then computes the prime factors. The heavy lifting of the component is actually going to be done by a third party, API. Within the component, we simply accept the input, pass it to this third party, API await the result of the computation, and then render out a simple ui.

01:07

Now this API will take a while to compute the result, which means that while we are waiting for the result, we cannot render the final ui and this will lead to a bad user experience. If you visit the website, we are left hanging and we see no user interface till that computation is complete. It would be great if while the computation is taking place, we can see the header of what we are trying to do along with a status indicator. I indicat that that computation is taking place. First, we create a loading placeholder for the prime factors component. This will be a dump component

01:38

that simply shows a box pulsating in place with the message computing prime factors. While that computation is taking place within our homepage, we bring in the suspense component from the React module. Suspense allows us to easily replace a component that is taking too long to render with a placeholder using the fallback prop. With this change in place, even while prime factors is still busy rendering, the server can immediately return the H one along with the prime factor loading ui. Let's verify this. By executing the code again within the browser, you can see

02:09

that the heading is already displayed along with the placeholder. While that component is still busy rendering on the server, and eventually when it comes back with a response, suspense seamlessly replaces the placeholder with the final ui.