Next.jsMedium

What is serialization and why does it matter when passing props?

Serialization converts values into a format that can be sent over the network. When a Server Component passes props to a Client Component, or a client calls a Server Function, React serializes the values into the RSC payload. Only types React can serialize can cross, and everything that crosses becomes visible to the browser.

Supported across the boundary

  • Primitives: string, number, bigint, boolean, null, undefined.
  • Arrays and plain objects (with serializable values).
  • Date, Map, Set, typed arrays, ArrayBuffer, FormData (for Server Function arguments).
  • Promises (resolved on the client with use()).
  • React elements (JSX) and Server Functions.

Not supported

  • Regular functions and arrow functions (event handlers).
  • Class instances with prototypes (ORM models, Decimal, URL, custom classes).
  • Symbols (except global ones), WeakMap, WeakSet, DOM nodes, React context.

Why it matters

  1. Correctness: passing a function or class instance throws an error during rendering.
  2. Security: props are embedded in the HTML and RSC payload. Anything you pass, including fields you do not render, is readable in the browser.
  3. Performance: large props increase payload size twice: once as HTML and once as data for hydration.
  4. Caching: "use cache" arguments and return values must also be serializable, and arguments become part of the cache key.

Practical rules

  • Map database results to plain DTOs with only the needed fields.
  • Convert Decimal to number or string, and IDs to strings.
  • Pass Server Functions instead of callbacks.
  • Use React's taint APIs to block sensitive objects from crossing.

Code example

tsx
1// app/invoices/page.tsx (Server Component)
2import { db } from "@/lib/db";
3import { InvoiceList } from "./invoice-list"; // "use client"
4
5export default async function Page() {
6 const rows = await db.invoice.findMany(); // ORM objects with Decimal amounts
7
8 const invoices = rows.map((r) => ({
9 id: r.id,
10 amount: r.amount.toNumber(), // Decimal class -> number
11 issuedAt: r.issuedAt, // Date is fine
12 status: r.status,
13 }));
14
15 return (
16 <InvoiceList
17 invoices={invoices}
18 // onSelect={(id) => console.log(id)} Error: functions cannot be serialized
19 // invoice={rows[0]} Error or leak: class instance with extra fields
20 />
21 );
22}

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app