Next.js Server Components vs Client Components: What Runs Where and When to Use Each

By Muhammad Osama Khan, Software Engineer. Next.js, Sep 29, 2026, 4 min read.

Components are server components by default. Learn what that changes, how to keep the client boundary small and the errors to watch for.

In the Next.js App Router, every component is a Server Component unless you say otherwise. It runs on the server, its code is never sent to the browser, and it can be an async function that talks to a database directly. A Client Component, marked with 'use client', is rendered to HTML on the server too, but it is then shipped to the browser and hydrated so it can react to clicks, state and effects.

A simple rule of thumb

  • Server Component for anything that only needs to produce output: fetching data, reading secrets, using heavy libraries (markdown, date handling), rendering static content.
  • Client Component when you need useState, useEffect, event handlers such as onClick, browser APIs (window, localStorage) or a library that depends on them.
  • Start on the server and move a piece to the client only when it needs interactivity.

Fetching data in a Server Component

There is no useEffect, no loading state to wire up and no API route to write just to read data. The component awaits the query and returns the result.

app/posts/page.tsx

import { db } from '@/lib/db';
import { LikeButton } from './LikeButton';

export default async function PostsPage() {
  const posts = await db.post.findMany({ orderBy: { createdAt: 'desc' }, take: 20 });

  return (
    <ul>
      {posts.map((post) => (
        <li key={post.id}>
          <h2>{post.title}</h2>
          <LikeButton postId={post.id} initialLikes={post.likes} />
        </li>
      ))}
    </ul>
  );
}

The database credentials and the query code stay on the server. Only the rendered result (and the small client component below) reaches the browser.

Adding interactivity with a small Client Component

app/posts/LikeButton.tsx

'use client';

import { useState } from 'react';

export function LikeButton({ postId, initialLikes }: { postId: string; initialLikes: number }) {
  const [likes, setLikes] = useState(initialLikes);
  const [pending, setPending] = useState(false);

  async function like() {
    setPending(true);
    const res = await fetch('/api/posts/' + postId + '/like', { method: 'POST' });
    if (res.ok) setLikes((n) => n + 1);
    setPending(false);
  }

  return (
    <button onClick={like} disabled={pending}>
      Like ({likes})
    </button>
  );
}

Keep the client boundary small

The 'use client' directive applies to the file and everything it imports. If you mark a whole page as a client component because one button needs state, all of that page ends up in the browser bundle. Push the directive down to the smallest component that needs it, as LikeButton does above.

Server content inside a Client Component

A Client Component cannot import a Server Component, but it can render one that is passed to it as children or another prop. This is how providers (theme, auth, store) wrap an app that is still mostly rendered on the server.

app/layout.tsx

import { ThemeProvider } from './ThemeProvider'; // a client component

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        <ThemeProvider>{children}</ThemeProvider>
      </body>
    </html>
  );
}

Mistakes that trip people up

  • Props crossing the boundary must be serialisable. You can pass strings, numbers, plain objects and arrays from a server to a client component, but not functions or class instances (Server Actions are the exception).
  • Hooks and context only work in Client Components. Using useState in a server file gives a build error; move that part into a client component.
  • Browser APIs do not exist on the server. Reading window during render fails. Read it inside an effect or an event handler in a client component.
  • Keep server-only code out of the browser. Import the server-only package in modules that hold secrets, and the build fails if a client component imports them by accident.

Frequently asked questions

Are components Server Components by default in Next.js?

Yes, in the App Router every component is a Server Component unless the file starts with 'use client'. Server Components run on the server and their code is not sent to the browser.

Can I use useState or useEffect in a Server Component?

No. Hooks that need state, effects or the browser only work in Client Components. Move the interactive part into a small component marked with 'use client' and keep the rest on the server.

Can a Client Component import a Server Component?

Not directly. But a Client Component can render a Server Component that is passed to it as children or another prop, which is how providers wrap a mostly server-rendered app.

Does "use client" mean a component only renders in the browser?

No. Client Components are still rendered to HTML on the server for the first load, and then hydrated in the browser so they become interactive.

Keep reading

All articles · Work with me

MO
Muhammad Osama Khan