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
- Load data in an async Server Component (database, API, file).
- Convert to plain data: map ORM models to objects, turn
DecimalorBigIntinto numbers or strings, and pick only the needed fields. - Render
<ClientThing data={plain} />. - 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"45export default async function TeamPage() {6 const users = await db.user.findMany({ include: { role: true } });78 // DTO: only safe, serializable fields cross the boundary9 const members = users.map((u) => ({10 id: u.id,11 name: u.name,12 role: u.role.name,13 joinedAt: u.createdAt, // Date is serializable14 // passwordHash: u.passwordHash <- never send15 }));1617 return <TeamTable members={members} />;18}1920// app/team/team-table.tsx21// "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.