Server Components vs Client Components in Next.js
Server Components render on the server and send no JavaScript; Client Components add interactivity. Learn when to use each and how to combine them.
By UndieGravity, , 2 min read
In the Next.js App Router every component is a Server Component unless you say otherwise. That one default changes how you structure a page. The short rule: fetch and render on the server, and move only the interactive parts to the client.
What a Server Component can do
- Run
asynccode and fetch data directly, including from a database. - Use secrets such as API keys, which never reach the browser.
- Import large libraries (markdown parsers, date libraries) without adding them to the client bundle.
What it cannot do: use useState, useEffect, event handlers like onClick, or browser APIs such as localStorage.
When you need a Client Component
Add "use client" at the top of a file when the component needs state, effects, event handlers, or browser APIs. Everything that file imports becomes part of the client bundle, so keep these files small.
"use client";
import { useState } from "react";
export function LikeButton() {
const [liked, setLiked] = useState(false);
return <button onClick={() => setLiked(!liked)}>{liked ? "Liked" : "Like"}</button>;
}Combine them: the leaf pattern
Keep the page and most of its markup as Server Components and place small Client Components at the leaves, such as a like button inside a server-rendered article.
// app/blog/[slug]/page.tsx (Server Component)
import { LikeButton } from "./LikeButton";
export default async function Page({ params }: PageProps<"/blog/[slug]">) {
const post = await getPost((await params).slug);
return (
<article>
<h1>{post.title}</h1>
<div>{post.body}</div>
<LikeButton />
</article>
);
}Passing server content into client components
A Client Component cannot import a Server Component, but it can receive one as children. This lets a client-side wrapper, like a modal or tabs, contain server-rendered content.
Rules for props
- Props passed from server to client must be serializable: strings, numbers, plain objects, arrays, dates.
- Functions cannot be passed, except Server Actions.
- Pass only the fields the client needs, not whole database rows, to keep the payload small.
A quick decision table
- Static text, layout, data from an API: Server Component.
- Forms with live validation, dropdowns, carousels, modals: Client Component.
- A form that just submits: a Server Component with a Server Action can be enough.