Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Passing Data from Server to Client Components | Section
Building Web Apps with Next.js

bookPassing Data from Server to Client Components

メニューを表示するにはスワイプしてください

One of the main advantages of Next.js is that you can fetch data on the server and then pass it to interactive components in the browser.

This allows you to combine the performance of Server Components with the interactivity of Client Components.

Example - Fetch Data on the Server

import Counter from "./Counter";

export default async function Page() {
  const data = { initialCount: 5 };

  return (
    <div>
      <h1>Dashboard</h1>
      <Counter initialCount={data.initialCount} />
    </div>
  );
}

This page is a Server Component. It prepares data and passes it as props.

Client Component Receiving Data

"use client";

import { useState } from "react";

export default function Counter({ initialCount }) {
  const [count, setCount] = useState(initialCount);

  return (
    <button onClick={() => setCount(count + 1)}>
      Count: {count}
    </button>
  );
}

The client component receives the data and uses it to initialize state.

How It Works

The Server Component runs first and prepares the data. Then the Client Component receives this data as props and becomes interactive in the browser.

This creates a clear flow:

  • Server → fetch and prepare data;
  • Client → display and interact with data.

Important Rule

Data passed from server to client must be serializable.

This means you can pass numbers, strings, arrays, objects.

You should not pass functions, class instances, complex non-serializable values.

Why This Pattern Is Important

This pattern allows you to:

  • Fetch data securely on the server;
  • Reduce client-side logic;
  • Keep performance high;
  • Still build interactive UI.

It is one of the most powerful patterns in Next.js.

question mark

How is data passed from a Server Component to a Client Component in Next.js?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  20

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  20
some-alt