Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readReact

Where to put the React client boundary

Put 'use client' on the interactive leaf of a Server Component page and pass serializable props from the server module.

Your page loads a seat count on the server, and you want a button that reveals it on click. Adding useState to the page triggers a Server Component error, while adding 'use client' pulls its data imports across the boundary. Put the state in the module that owns the button.

Keep the data read on the server

In a Next.js App Router project, put this route in app/page.jsx. It reads the count and passes it to the toggle as a number.

import { readSeats } from './read-seats'
import { SeatToggle } from './seat-toggle'

export default async function Page() {
  const seats = await readSeats()

  return (
    <main>
      <h1>Hall A</h1>
      <SeatToggle seats={seats} />
    </main>
  )
}

The parent renders the hall heading and passes the data needed for the toggle.

Put {"seats":12} in seats.json at the project root. Save the helper as app/read-seats.js. The server-only marker lets Next.js report a build error if Client Component code imports the helper.

import 'server-only'
import { readFile } from 'node:fs/promises'
import { join } from 'node:path'

export async function readSeats() {
  const file = join(process.cwd(), 'seats.json')
  return JSON.parse(await readFile(file, 'utf8')).seats
}

The route reads the fixture on the server and gets the number 12.

Put use client on the toggle

Save this as app/seat-toggle.jsx. Place the directive before its imports.

'use client'

import { useState } from 'react'

export function SeatToggle({ seats }) {
  const [open, setOpen] = useState(false)

  return (
    <>
      <button
        type="button"
        aria-expanded={open}
        aria-controls="seat-count"
        onClick={() => setOpen(value => !value)}
      >
        {open ? 'Hide seat count' : 'Show seat count'}
      </button>
      <p id="seat-count" hidden={!open}>{seats} seats left</p>
    </>
  )
}

Open the page and press Show seat count to reveal 12 seats left.

diagram

The page keeps its server imports

The page imports both modules, and the toggle receives the seat count as a prop.

React uses the directive to mark a boundary in the module dependency graph. The client module graph includes seat-toggle.jsx and the modules it imports, including indirect imports. Only the server page imports read-seats.js. The toggle receives the number across the boundary as a prop.

Next.js still renders Client Components to HTML on the server for the initial load, then runs their JavaScript in the browser to attach handlers. Keep file reads in the Server Component and put browser-only work in a handler or an effect.

If you get a server-only import error, follow imports starting at the 'use client' file. A shared helper can pull in the file reader even if the toggle never calls it.