React Serializable Types

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
🚨 Work In Progress 🚧
Subscribe for Launch

React Serializable Types

Subscription Required

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

Core Concept

There are a limited number of types that react can safely serialize between the server and the client.

This applies to:

  • server component → props → client component
  • client component → argument → server function
  • server function → return → client component

JSON is Limited

Note that while it is a limited set of types, it is still more than the ones supported with just JSON.

The limitations of JSON are demonstrated below, we cannot even serialize Date objects reliably.

app/api/route.ts
app/page.tsx
import { NextRequest, NextResponse } from "next/server";

export async function POST(req: NextRequest) {
const body = await req.json();

console.log({
isDate: body.date instanceof Date,
type: typeof body.date,
value: body.date,
});

return NextResponse.json({
date: new Date(),
});
}

Demo of the Seamless Nature of Serialization

The following demo shows how React seamlessly supports common JavaScript types (like Date) with its built in serialization. The date makes it way to the server and back with no loss of type information.

lib/functions.ts
app/page.tsx
"use server";

export async function serverFunction(body: { date: Date }) {
console.log({
isDate: body.date instanceof Date,
type: typeof body.date,
value: body.date,
});

return {
date: new Date(),
};
}

Error on Inappropriate Types

React will error out if you try to pass in an object that is not supported by its built-in serialization.

As an example it will error out if we try to pass a custom class from a server component to a client component prop.

lib/Counter.ts
app/page.tsx
lib/Client.tsx
export class Counter {
count = 0;
inc() {
this.count++;
}
}

Reference

Here is a simplified reference to which types you can safely serialize.

For both use client (props) and use server (arguments / return):

  • Primitives
    • string
    • number
    • bigint
    • boolean
    • undefined
    • null
    • Symbol **
  • Common Types
    • Date
    • Promise
    • Server Functions **
  • Composite types, with serializable values
    • Object
    • Array
    • Map
    • Set
    • TypedArray
    • ArrayBuffer

For use client only:

  • JSX Elements (e.g. other components passed from server component to client component as props)

**: Don’t recommend using these as they have caveats. No point in memorizing those caveats and instead consider them as things that are supported internally by React but you shouldn’t rely on these directly.

javascript
typescript
react
playwright

Enjoy free content straight from your inbox 💌

No spam, unsubscribe at any time.

Transcript

00:00

There are a limited number of types that react can safely serialize between the server and the client. The applies to props passed from your server components to your client components. Also for server function arguments and even for server function return values. Note that while it is a limited set of types, it is still more than the ones supported with just J. To demonstrate the limitations of JSON serialization, we create a simple API that accepts a J payload and that payload is going to have a member date, which we expect to be of type date because that is what we are going to pass from the client.

00:33

Similarly, as a part of the response, we are going to return a member date of type date with our API in place. Let's invoke it from a client page. We make a request with the body containing date of type date, and then we receive the response and see if the return J payload contains a date or not. With our page created, let's take a look at it within the browser. If you've watched a JavaScript course or have worked with JSON before, you probably know the answer anyways. On the server logs, we can see that it is not a date. It is in fact a string because that is how the JavaScript date object serializes with J, and the same is true

01:07

for the date returned from the server to the client. It doesn't come through as a date and gets serialized to a string. Let's take a look at how React seamlessly supports common JavaScript types. With its built-in serialization, instead of creating an API, we create a server function that expects to be passed in an object containing a date. We log out the types same as before and same as before. We try to return a date back from the server on the client component. We import the server function and then invoke it. When our component gets mounted as a part of the payload, we pass in a date object to the server, and then from the response we check if we

01:40

indeed get a date back. If you run this application within the browser, you can see on the console that indeed we do get a date back from the server and similarly, if you look at the server console, the server did indeed receive a date object from the client. Let's take a look at what happens if you try to serialize the data structure that isn't supported. For this demonstration, we create a simple counter class that maintains account value. Now let's see if this is something that can be serialized by React within a server component. We create an instance of this counter class and then try to pass it to a client component as a prop. The client component will accept the instance of this class

02:14

as a prop and then try to use the increment function. I think you can already imagine that this is not going to work, but let's take a look at it within the browser. React handles this particular error quite gracefully and informs us that classes are not supported. Only plain objects and a few built-in scan be passed to client components from several components. As a final note, you can find a simplified reference to which types you can safely serialize in the lesson description. Thank you for joining me and I'll see you in the next one.

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
🚨 Work In Progress 🚧
Subscribe for Launch