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.

Professional NextJS Lessons

1.Course Intro
free
⏱️ 2:05
2.Get Setup
free
⏱️ 2:05
3.Project Template
free
⏱️ 5:45
4.Introducing FullStack React
free
⏱️ 4:57
5.App Router Reference
free
⏱️ 7:08
6.NextJS Server vs. "use client" Rendering
⏱️ 5:01
7.Nesting Server and Client Components
⏱️ 8:51
8.Client Only Components
⏱️ 3:48
9.Global CSS in NextJS
⏱️ 6:30
10.CSS Modules and SASS
⏱️ 4:15
11.Tailwind CSS
⏱️ 5:17
12.NextJS Custom Fonts
⏱️ 4:08
13.Static vs Dynamic Rendering
⏱️ 4:55
14.NextJS Link Component
⏱️ 3:20
15.NextJS Link Prefetching
⏱️ 5:49
16.Navigation Scroll with Link
⏱️ 2:02
17.Replace Navigation History
⏱️ 1:35
18.NextJS Image Component
⏱️ 7:01
19.Dynamic Image URLs
⏱️ 5:34
20.Safe Colocation with Private Folders
⏱️ 2:12
21.NextJS Route Groups
⏱️ 3:07
22.Dynamic Routes
⏱️ 5:37
23.NextJS API Routes
⏱️ 6:03
24.NextRequest and NextResponse
⏱️ 2:41
25.Cookies in API Handlers
⏱️ 4:25
26.React Suspense Simplified
⏱️ 2:23
27.Power of Suspense Boundaries
⏱️ 3:52
28.use Promises
⏱️ 3:24
29.Stream Promise Results from Server to Client
⏱️ 3:03
30.use Requires a Cached Promise
free
⏱️ 2:30
31.Suspense Error Boundaries
⏱️ 4:42
32.React Server Functions
⏱️ 4:50
33.Server Actions for Powerful Forms
⏱️ 5:35
34.useActionState to Simplify Form Actions
⏱️ 6:38
35.React Serializable Types
⏱️ 2:42
36.Create Dynamic Images with NextJS ImageResponse
⏱️ 5:54
37.Deploy NextJS Application to Production
⏱️ 3:51
38.NextJS Environment Variables Masterclass
⏱️ 3:50
39.Deploying and Managing Environment Variables
⏱️ 4:38
40.Using Databases for Dynamic Server Side State
free
⏱️ 7:34
41.Mastering NextJS Layout Files
⏱️ 4:54
42.Client State Management with NextJS
⏱️ 3:48
43.Client State Management In Action
⏱️ 8:04
44.Using Client State Management Libraries
⏱️ 5:25
45.Detecting Server vs Client Runtime and useEffect
⏱️ 4:33
46.Demystifying ISR vs SSR vs SSG
⏱️ 3:07
47.Static Site Generation or SSG with NextJS
⏱️ 3:16
48.Incremental Static Regeneration (ISR) Simplified
⏱️ 2:51
49.API Routes Caching with NextJS
⏱️ 3:05
50.Reference Guide to Caching for Modern NextJS
⏱️ 2:47
51.Setup a BASE_URL for client and server
free
⏱️ 2:54
52.Creating a sitemap.xml
⏱️ 3:29
53.Provide metadata with NextJS
⏱️ 6:54
54.Created High Quality Social Media Previews
⏱️ 7:38

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.

Professional NextJS

Professional NextJS

1.Course Intro
free
⏱️ 2:05
2.Get Setup
free
⏱️ 2:05
3.Project Template
free
⏱️ 5:45
4.Introducing FullStack React
free
⏱️ 4:57
5.App Router Reference
free
⏱️ 7:08
6.NextJS Server vs. "use client" Rendering
⏱️ 5:01
7.Nesting Server and Client Components
⏱️ 8:51
8.Client Only Components
⏱️ 3:48
9.Global CSS in NextJS
⏱️ 6:30
10.CSS Modules and SASS
⏱️ 4:15
11.Tailwind CSS
⏱️ 5:17
12.NextJS Custom Fonts
⏱️ 4:08
13.Static vs Dynamic Rendering
⏱️ 4:55
14.NextJS Link Component
⏱️ 3:20
15.NextJS Link Prefetching
⏱️ 5:49
16.Navigation Scroll with Link
⏱️ 2:02
17.Replace Navigation History
⏱️ 1:35
18.NextJS Image Component
⏱️ 7:01
19.Dynamic Image URLs
⏱️ 5:34
20.Safe Colocation with Private Folders
⏱️ 2:12
21.NextJS Route Groups
⏱️ 3:07
22.Dynamic Routes
⏱️ 5:37
23.NextJS API Routes
⏱️ 6:03
24.NextRequest and NextResponse
⏱️ 2:41
25.Cookies in API Handlers
⏱️ 4:25
26.React Suspense Simplified
⏱️ 2:23
27.Power of Suspense Boundaries
⏱️ 3:52
28.use Promises
⏱️ 3:24
29.Stream Promise Results from Server to Client
⏱️ 3:03
30.use Requires a Cached Promise
free
⏱️ 2:30
31.Suspense Error Boundaries
⏱️ 4:42
32.React Server Functions
⏱️ 4:50
33.Server Actions for Powerful Forms
⏱️ 5:35
34.useActionState to Simplify Form Actions
⏱️ 6:38
35.React Serializable Types
⏱️ 2:42
36.Create Dynamic Images with NextJS ImageResponse
⏱️ 5:54
37.Deploy NextJS Application to Production
⏱️ 3:51
38.NextJS Environment Variables Masterclass
⏱️ 3:50
39.Deploying and Managing Environment Variables
⏱️ 4:38
40.Using Databases for Dynamic Server Side State
free
⏱️ 7:34
41.Mastering NextJS Layout Files
⏱️ 4:54
42.Client State Management with NextJS
⏱️ 3:48
43.Client State Management In Action
⏱️ 8:04
44.Using Client State Management Libraries
⏱️ 5:25
45.Detecting Server vs Client Runtime and useEffect
⏱️ 4:33
46.Demystifying ISR vs SSR vs SSG
⏱️ 3:07
47.Static Site Generation or SSG with NextJS
⏱️ 3:16
48.Incremental Static Regeneration (ISR) Simplified
⏱️ 2:51
49.API Routes Caching with NextJS
⏱️ 3:05
50.Reference Guide to Caching for Modern NextJS
⏱️ 2:47
51.Setup a BASE_URL for client and server
free
⏱️ 2:54
52.Creating a sitemap.xml
⏱️ 3:29
53.Provide metadata with NextJS
⏱️ 6:54
54.Created High Quality Social Media Previews
⏱️ 7:38