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

use Requires a Cached Promise

Don't use with uncached promise

A common mistake that a lot of people are making with the modern use api in react is creating a promise and wrapping it with use in the same statement. This is incorrect and actually results in an infinite loop.

As an example the following code will cause an infinite loop because of use(fetch()) style code.

lib/SquareRootResult.tsx
lib/SquareRoot.tsx
"use client";

import { fetchSqrt } from "./api";
import { use } from "react";

export const SquareRootResult = ({ input }: { input: number }) => {
const result = use(fetchSqrt(input));
return <div>{result.toFixed(2)}</div>;
};

Fix

There are a number of ways to fix this issue. A simple approach is:

  • Lift the promise into the parent component.
  • Ensure that the promise remains the same (referential equal) for a given input.

This fix is demonstrated in the following code:

lib/SquareRootResult.tsx
lib/SquareRoot.tsx
"use client";

import { use } from "react";

export const SquareRootResult = ({ input }: { input: Promise<number> }) => {
const result = use(input);
return <div>{result.toFixed(2)}</div>;
};
javascript
typescript
react
playwright

Enjoy free content straight from your inbox 💌

No spam, unsubscribe at any time.

Transcript

00:00

A common mistake that a lot of people are making with the modern use API and React is creating a promise and wrapping it in use in the same statement. This is incorrect and can actually result in an infinite loop. To demonstrate the issue, we create a simple client component that is going to accept a value from the user, and for this purpose we create a state variable followed by an input wired to that state. Finally, we pass the user selected value into a square root result component. This component is going to make an API call to compute the square root of a given input.

00:34

We make the API call using fed square root, which gives best back a promise which we unwrap using use. Now that we have the square root, we simply display it within a dev. You would think that this would work. We make an API call unwrap it with use and then display the value. However, when we look at the application within the browser, sinister things will happen. Initially it seems to work fine, but on the console you can see that the calls to fit square root are being made again and again. And in fact, if you modify the value provided to that component, the calls seem

01:06

to increase in frequency. Additionally, they get stuck in calculating and we never see the final answer. Just as a thought experiment. Based on your knowledge of react, can you guess why this is not working? The answer lies in the fact that we are creating the promise within this component, which means that every single time this component renders, it'll create a new API call. And once that call is resolved, it triggers another re agenda, which means that we are stuck in an infinite loop. There are a number of ways to fix this issue, so let's take a look at one simple approach. Instead of creating the API call, within this component,

01:40

we modify our props to be a promise of the result of this API, and within the component, we simply wait for that promise to resolve with use. That's all we need to do for this component. The next step would be to create the promise for a given value. Within the parent component. We create the promise within use memos. Whenever the value changes, we will calculate a new promise and finally pass this promise down to the child component. We are now creating the promise based on the value caching that within the parent, and then the child waits for it to resolve with use. Now, let's run the example to see if it fixes our issues.

02:13

We open up the browser and you can see that a single call to FET script was made. If you modify the value, for example, to any other thing, a new call is made, but that is all. We don't end up in an infinite loop.

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