Skip to content
jshacks.dev
Frameworks

Pass server data to client components as props

Load data in a Next.js 16 Server Component and hand it to a Client Component as serializable props, instead of fetching it again in the browser.

1 min read
Summarize in AI

A Client Component that needs data can fetch it in an effect, which costs a second round trip. It is better to load it on the server and pass it in.

app/cart/page.jsJSX
import { CartList } from './CartList';
import { getCart } from '@/lib/cart';

export default async function CartPage() {
  const items = await getCart();

  return <CartList initialItems={items} />;
}
app/cart/CartList.jsJSX
'use client';

import { useState } from 'react';

export function CartList({ initialItems }) {
  const [items, setItems] = useState(initialItems);

  return (
    <ul>
      {items.map((item) => (
        <li key={item.id}>
          {item.name}
          <button onClick={() => setItems(items.filter((i) => i.id !== item.id))}>
            Remove
          </button>
        </li>
      ))}
    </ul>
  );
}
Text
Pen    [Remove]
Ink    [Remove]       (in the first HTML response, before any client fetch)

The server renders the list with real data, so the visitor sees it at once, and the client takes over for the interactive part, seeded through initialItems. Props crossing the boundary must be serializable: strings, numbers, plain objects and arrays, Date, Map, Set and promises. Functions and class instances are not allowed, except Server Functions. See Passing props from Server to Client Components.

Never pass secrets, such as a full user record with a password hash, because everything in the props ends up in the page payload.

Taggednextjs 16
Share

More snippets

Frameworks

Update state from the previous value

Pass an updater function to the React state setter when the next value depends on the last one, so batched updates do not overwrite each other.

1 min read
Frameworks

Manage state with useState

Keep a changing value in a React 19 component with useState, and see why the setter schedules a re-render instead of changing the variable.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.