Menu

Composition Patterns and the Server Client Boundary

Lesson 3: Composition Patterns and the Server–Client Boundary

What can cross the boundary?

Props passed from a Server Component to a Client Component are sent over the network, so they must be serialisable.

Prop type Allowed?
Strings, numbers, booleans, null Yes
Plain objects and arrays Yes
Date, Map, Set Yes
Promises Yes (read them with React's use() hook)
JSX / Server Components as children Yes
Server Actions (functions with 'use server') Yes
Regular functions, class instances No

Pattern 1: Server Components as children of Client Components

A Client Component cannot import a Server Component, but it can receive one as children.

// Modal.tsx
'use client';
import { useState } from 'react';

export function Modal({ children }: { children: React.ReactNode }) {
  const [open, setOpen] = useState(false);
  return (
    <>
      <button onClick={() => setOpen(true)}>View syllabus</button>
      {open && <div role="dialog">{children}</div>}
    </>
  );
}
// page.tsx (Server Component)
<Modal>
  <Syllabus courseId={course.id} /> {/* Still a Server Component */}
</Modal>

Pattern 2: Context providers

React context only works in Client Components. Wrap providers in their own client file and use it in a layout:

// app/providers.tsx
'use client';
import { ThemeProvider } from './theme-context';

export function Providers({ children }: { children: React.ReactNode }) {
  return <ThemeProvider>{children}</ThemeProvider>;
}

The layout stays a Server Component, and its children can still be Server Components.

Pattern 3: Keep server code on the server

// lib/data.ts
import 'server-only';

export async function getRevenueReport() {
  // Uses a secret key – must never be bundled for the browser
}

If a Client Component imports this file by mistake, the build fails with a clear error instead of leaking code. Install it with npm install server-only.

How it works

  • The server renders Server Components first and leaves "holes" where Client Components go.
  • children passed into a Client Component are already rendered on the server, so their code never reaches the browser.
  • Only variables prefixed with NEXT_PUBLIC_ are available in Client Components. Others are undefined there.

Common mistakes

Mistake Effect Fix
Passing a function such as onSelect={handle} from server to client Serialisation error Define the handler in the Client Component, or pass a Server Action
Importing a Server Component into a client file It silently becomes a Client Component Pass it as children or a prop instead
Passing a whole database record Leaks private fields such as emails Pass only the fields the UI needs
Wrapping the root layout's body in a large client tree Server benefits lost Keep providers thin and accept children

Key takeaways

  • Props crossing the boundary must be serialisable.
  • Compose by passing Server Components as children.
  • Use server-only to guarantee secrets stay on the server.

Try it: Create a client Tabs component and pass three server-rendered panels (Overview, Syllabus, Reviews) into it as props.


Nextjs Tutorial

J

Jebasta





Get in Touch For Details