learn deep. ship safe.Practice testsFollow ByteMunSubscribe

Concept

React Server Components

Components that run only on the server and send rendered output, not their code, to the browser.

React Server Components explained

React Server Components are components that run only on the server. They can read databases and files directly, and they send their rendered result to the browser, not their code. Client components, marked with "use client", are the ones that ship JavaScript and can use state, effects and event handlers. Mixing the two lets most of a page stay server-rendered with no client JavaScript, while interactive parts remain fully interactive.

How server and client components fit together

// app/posts/page.jsx: a server component (the default in frameworks that support them)
export default async function Posts() {
  const posts = await db.posts.findMany();   // runs on the server only
  return <ul>{posts.map((p) => <Post key={p.id} post={p} />)}</ul>;
}

// LikeButton.jsx: a client component
"use client";
export function LikeButton({ id }) {
  const [liked, setLiked] = useState(false);
  return <button onClick={() => setLiked(!liked)}>{liked ? "Liked" : "Like"}</button>;
}

What each kind can do

  • Server components: async/await, direct data access, secrets stay on the server, zero bundle size. No state, effects or browser APIs.
  • Client components: hooks, events, browser APIs. Their code is sent to the browser.
  • A server component can render a client component and pass it serialisable props. Anything a client component imports becomes client code too, so to keep a component on the server, pass it to the client component as children (or another JSX prop) instead of importing it there.

Why they help

  • Less JavaScript to download and run, which improves load time and responsiveness.
  • Data fetching sits next to the component that needs it, without an extra API layer.
  • Sensitive logic and credentials never reach the browser.

Common mistakes

  • Putting "use client" at the top of the tree, which turns everything below into client code.
  • Passing ordinary functions or class instances from server to client components. Props must be serialisable; the only functions allowed are Server Functions (marked 'use server'), and event handlers such as onClick belong in client components.
  • Importing server-only code (database clients) into a client component.

What interviewers ask about server components

  • "Server components versus server-side rendering?" SSR renders HTML for the first load; server components are never sent to the client as code.
  • "Where would you put the "use client" boundary?" As low as possible, around the interactive part.
Concept map
Part of ReactReact Server Components
Then learnServer actions

What should you learn before React Server Components?

  1. StateData that changes over time and makes the UI re-render, and where to keep it.
  2. RenderingWhen and why components render, and what triggers a re-render.
  3. Server-side rendering and hydrationRendering HTML on the server, then attaching interactivity in the browser.

What to learn after React Server Components

  • Server actionsFunctions called from the client that run on the server, used for form submissions and mutations.

Connections to other concepts

Part of
React

Practise React Server Components

All topics