React Server Components and Server Actions

Components that render on the server with direct data access and zero client JavaScript, combined with interactive client components.

Advanced · about 6 hours · difficulty 4/5

Learn first: Suspense, Error Boundaries and Concurrent Features

React Server Components (RSC) split your tree into two kinds of components. Server Components run only on the server (at build time or per request); they can be async, read databases or files directly, keep secrets on the server and send no JavaScript to the browser. Client Components, marked with "use client" at the top of a file, are the familiar interactive components with state and effects.

You use RSC through a framework, most commonly the Next.js App Router.

The mental model

  • Components are Server Components by default. Add "use client" only at the boundary where interactivity starts (a button, a form, a chart).
  • Server Components can render Client Components and pass them serialisable props (plain data, not functions or class instances).
  • Client Components cannot import Server Components, but can receive them as children.
  • Keep the client boundary low in the tree so most of the page ships as HTML.

Server Actions

Functions marked "use server" can be called from forms or client code; the framework turns them into secure POST endpoints. Treat them like public API routes: validate input and check authorisation inside every action.

Benefits and trade-offs

Smaller bundles, no client-side fetch waterfalls for initial data, and secrets that never reach the browser. The cost is a new mental model, caching behaviour to understand, and libraries that rely on client-only APIs needing a client wrapper.

Good enough to move on

You can build a page that fetches data in an async Server Component, adds interactivity through a small Client Component, and mutates data with a validated Server Action.

Key concepts

  • Server vs Client Components
  • The use client boundary
  • Async components and server data access
  • Serialisable props and passing children
  • Server Actions and use server
  • Validating and authorising actions
  • Streaming with Suspense

Mini project

In a Next.js App Router project, build a notes app where the list is an async Server Component reading from a database, a search box is a Client Component, and create/delete use validated Server Actions.

Check yourself

Why can you not pass an onClick function prop from a Server Component to a Client Component?

Free resources