Next.jsMedium

How do you pass server data as props to Client Components?

Fetch or compute the data in a Server Component, shape it into plain serializable values, and pass it as props to the Client Component. React serializes the props into the RSC payload, so only include what the client needs.

Steps

  1. Load data in an async Server Component (database, API, file).
  2. Convert to plain data: map ORM models to objects, turn Decimal or BigInt into numbers or strings, and pick only the needed fields.
  3. Render <ClientThing data={plain} />.
  4. Optionally pass a promise instead of awaited data to stream it, and read it with use().

Serializable types

Strings, numbers, booleans, null, undefined, arrays, plain objects, Date, Map, Set, typed arrays, BigInt, promises, React elements, and Server Functions. Not allowed: regular functions, class instances, symbols.

Size and privacy

  • Props are embedded in the HTML and RSC payload. A 2MB product list is sent twice in effect (as HTML and as payload). Paginate or summarize.
  • Everything is readable in the browser's view-source. Never pass password hashes, tokens or internal IDs you do not want public.
  • Create a Data Transfer Object (DTO) layer that returns safe shapes.

Updating after load

Props are a snapshot. To refresh them, call router.refresh(), revalidate in a Server Function, or let SWR/TanStack Query take over using the props as initial data.

Code example

tsx
1// app/team/page.tsx (Server Component)
2import { db } from "@/lib/db";
3import { TeamTable } from "./team-table"; // "use client"
4
5export default async function TeamPage() {
6 const users = await db.user.findMany({ include: { role: true } });
7
8 // DTO: only safe, serializable fields cross the boundary
9 const members = users.map((u) => ({
10 id: u.id,
11 name: u.name,
12 role: u.role.name,
13 joinedAt: u.createdAt, // Date is serializable
14 // passwordHash: u.passwordHash <- never send
15 }));
16
17 return <TeamTable members={members} />;
18}
19
20// app/team/team-table.tsx
21// "use client";
22// export function TeamTable({ members }: { members: { id: string; name: string; role: string; joinedAt: Date }[] }) {
23// const [query, setQuery] = useState("");
24// const visible = members.filter((m) => m.name.toLowerCase().includes(query.toLowerCase()));
25// ...
26// }

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