# React Server Components (RSCs)

> A paradigm shift where components run exclusively on the server, never shipping JavaScript to the browser.

Subject: React | Lesson 46 of 48 | 6 min read
Interactive lesson: https://codewithanimation.in/react/react-server-components
Previous: [Rendering Patterns](https://codewithanimation.in/react/rendering-patterns.md) | Next: [Server Actions](https://codewithanimation.in/react/server-actions.md)

## What are Server Components?

Historically, all React components were **Client Components**. Even if they were initially rendered on the server (SSR), their code was eventually sent to the browser to be hydrated and executed.

**React Server Components (RSCs)** are a new type of component that only runs on the server. They resolve their data and return a special UI format (not HTML, but a serialized React tree). Their JavaScript code is *never* sent to the client.

## Zero-Bundle Size and Direct DB Access

Because RSC code never reaches the browser, you can import massive backend libraries (like a markdown parser or a database ORM) without increasing the user's bundle size by a single byte.

Furthermore, because they run securely on the server, you can write async components that talk directly to your database without needing to create a middleman API route: `const users = await db.query('SELECT * FROM users')`.

## Mixing Server and Client Components

Server Components are fantastic for data fetching and heavy lifting, but they have a massive limitation: **they cannot have state or interactivity**. You cannot use `useState`, `useEffect`, or `onClick` in a Server Component.

To build interactive apps, you interleave them. You use Server Components for the layout and data-fetching shell, and you import smaller, targeted **Client Components** (marked with the `'use client'` directive) for the specific parts of the UI that need interactivity (like a button or a form).

## Common mistake: The Network Boundary

The boundary between a Server Component and a Client Component is a network boundary. This means you must be very careful about the *props* you pass from Server to Client.

You cannot pass functions (like event handlers) or complex non-serializable objects from a Server Component to a Client Component, because those props have to be serialized over the network.

## Tip: Push Client Components down the tree

A common mistake when learning RSCs is to put `'use client'` at the very top of your application (like on your Layout). This turns your entire app back into a legacy Client Component app.

Always push `'use client'` as far down the component tree as possible. If only the 'Like Button' needs state, only the 'Like Button' should be a Client Component.

## Try it: The Bundle Size Paradigm

Compare a traditional Client Component (fetching data) with a modern Server Component. Notice what happens to the JavaScript bundle size when importing heavy backend dependencies.

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

## Check yourself

### Question 1

How much JavaScript is added to the browser's bundle when you use a React Server Component?

- A. Zero bytes. The code never leaves the server.
- B. It depends on how many `useState` hooks are inside the component.
- C. Only the hydration logic is sent to the client.

Correct answer: A

RSCs execute entirely on the server and send a serialized UI payload to the browser. The actual component function, and any libraries it imports, are never sent to the client.

### Question 2

Which of the following can you NOT use inside a React Server Component?

- A. `async` / `await`
- B. `useState` and `onClick`
- C. Direct database connections (e.g., `db.query()`)

Correct answer: B

Because Server Components run once on the server and are never hydrated in the browser, they cannot react to user events or hold state. For those features, you must use a Client Component.

### Question 3

How do you tell React that a component should be a Client Component (able to use state) in modern frameworks like Next.js App Router?

- A. By adding the `'use client'` directive at the top of the file.
- B. By importing the component from `'react-dom/client'`.
- C. By wrapping it in a `<ClientProvider>`.

Correct answer: A

In modern React, components are Server Components by default. You must explicitly opt-in to client-side interactivity by placing `'use client'` at the top of the file.

## Remember this

- Server Components run exclusively on the server and never ship JavaScript to the browser.
- You can securely use direct database connections and secret API keys inside Server Components.
- Server Components cannot use state, effects, or DOM event listeners.
- Client Components (marked with `'use client'`) handle interactivity and state.
- Pass data from Server Components to Client Components via props (must be serializable).
