# Server vs Client Components

> The fundamental divide in the App Router: where does your code actually run?

Subject: Next.js | Lesson 1 of 65 | 5 min read
Interactive lesson: https://codewithanimation.in/nextjs/server-vs-client-components
Next: [App Router File-System Routing](https://codewithanimation.in/nextjs/app-router-routing.md)

## What are Server and Client Components?

In the Next.js App Router, React components can run in two completely different environments: on the server, or in the user's web browser (the client).

By default, every component you write is a Server Component. It renders into static HTML on the server. The actual JavaScript code that powers it is never sent to the browser, resulting in a zero-kilobyte bundle size for that piece of UI.

Client Components, on the other hand, are the traditional React components you are used to. They are bundled, sent over the network, and executed in the browser to enable interactivity, state, and event listeners.

## Why split them up?

Before React Server Components, you had to make a painful choice. You either sent all your component code to the client (bloating the bundle size and slowing down the initial load), or you used complex fetching patterns to render HTML early.

This architecture lets you have the best of both worlds. You can write heavy, data-fetching UI on the server, and only ship JavaScript to the client for the specific buttons, forms, and animations that actually need it.

## How do you use them?

You don't have to do anything to create a Server Component; it is the default behavior. Just write a normal React function.

To create a Client Component, you must add the `"use client"` directive at the absolute top of the file, before any imports.

This directive acts as a network boundary. When Next.js sees `"use client"`, it bundles that file and every file it imports into the client-side JavaScript payload.

## Common mistake: The 'use client' virus

The most common mistake beginners make is putting `"use client"` at the very top of their page layout or root component just so they can use a single click handler.

Because `"use client"` forces all imported children to also become client components, placing it too high up the tree completely defeats the purpose of the App Router, forcing your entire application to be bundled and shipped to the user's browser.

## Tip: The 'children' loophole

You cannot import a Server Component directly into a Client Component. If you do, it automatically becomes a Client Component.

However, you CAN pass a Server Component as a `children` prop to a Client Component. This allows you to wrap interactive client-side context providers around static server-rendered content without bloating the bundle.

## Try it: The Network Boundary

Toggle the environment to see how code execution moves across the network boundary, and watch the impact on the client-side JavaScript bundle.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/server-vs-client-components

## Check yourself

### Question 1

What is the default component type in the Next.js App Router?

- A. Server Components
- B. Client Components
- C. Static Components
- D. Hydrated Components

Correct answer: A

By default, every component in the App Router is a React Server Component. You must explicitly opt into client-side execution.

- Why not B: Client components must be explicitly opted into using a directive.
- Why not C: While they output static HTML, the correct React terminology is Server Components.
- Why not D: Hydration only happens for Client Components.

### Question 2

Which of the following requires a component to use the 'use client' directive?

- A. Fetching data from a database
- B. Using the useState hook
- C. Reading a local file with fs
- D. Exporting an async function

Correct answer: B

State (`useState`), lifecycle effects (`useEffect`), and DOM event listeners (`onClick`) require a browser environment, meaning they must be in a Client Component.

- Why not A: Database access is exactly what Server Components are designed for.
- Why not C: The 'fs' module only exists on the server.
- Why not D: Server Components can be async. Client components cannot.

### Question 3

What happens if you directly import a Server Component into a Client Component?

- A. It throws a build error
- B. It remains a Server Component
- C. It is silently bundled as a Client Component
- D. It creates a React Context

Correct answer: C

The `"use client"` directive marks a boundary. Everything imported beneath that boundary, even if it doesn't have the directive itself, is bundled and shipped to the client.

- Why not A: Next.js will not throw an error, it will just bundle it.
- Why not B: The boundary has already been crossed; the code must go to the client.
- Why not D: Context is a separate feature entirely.

## Remember this

- All components in the App Router are Server Components by default.
- Use the `"use client"` directive only at the leaves of your component tree.
- Server Components cannot use hooks like `useState` or attach event listeners.
- Importing a Server Component into a Client Component turns it into a Client Component.
