How do you pass data from Server Components to Client Components?
Fetch data in a Server Component and pass it to a Client Component as props. The props must be serializable by React (plain objects, arrays, strings, numbers, Dates, Maps, Sets, promises, JSX and Server Functions), because they travel to the browser inside the RSC payload.
Patterns
- Props: the simplest.
<Chart data={rows} />whereChartis"use client". - Pass a promise: start the fetch on the server without awaiting it, pass the promise, and read it in the client with React's
use()inside<Suspense>. The page streams instead of waiting. - Children / slots: pass server-rendered JSX into a Client Component through
children, so the server part never becomes client code. - Server Functions as props: pass a
"use server"function, for exampleonSave, and call it from the client.
What you cannot pass
- Regular functions and event handlers (except Server Functions).
- Class instances (a database model object,
Decimal), unless converted to plain data. - Symbols, WeakMaps, DOM nodes.
Security
Everything you pass is visible in the browser. Map database rows to a DTO with only the fields the UI needs, and never pass secrets. React's taint APIs (experimental.taint in next.config) can block specific objects or values from being sent.
Shared data
For data many client components need, fetch on the server and pass it into a context provider rendered in the layout.
Code example
1// app/sales/page.tsx (Server Component)2import { Suspense } from "react";3import { SalesChart } from "./sales-chart";4import { RecentTable } from "./recent-table";5import { getSales, getRecent } from "@/lib/sales";67export default async function SalesPage() {8 const sales = await getSales(); // awaited: passed as plain data9 const recentPromise = getRecent(); // not awaited: streamed as a promise1011 return (12 <main>13 <SalesChart points={sales.map((s) => ({ day: s.day, total: Number(s.total) }))} />14 <Suspense fallback={<p>Loading recent orders…</p>}>15 <RecentTable ordersPromise={recentPromise} />16 </Suspense>17 </main>18 );19}2021// app/sales/recent-table.tsx22// "use client";23// import { use } from "react";24// export function RecentTable({ ordersPromise }: { ordersPromise: Promise<Order[]> }) {25// const orders = use(ordersPromise); // suspends until the server resolves it26// return <table>{orders.map((o) => <tr key={o.id}><td>{o.total}</td></tr>)}</table>;27// }
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.