Skip to the solution
MediumFrontend7 min read

What is the 'use client' directive and when do you need it?

94 views

The problem

Question presented to candidate: "Your teammate put 'use client' at the top of the root layout to make an error go away. What does the directive actually do, and where should it go instead?"

What a strong answer should cover:

  • 'use client' declares a boundary between the server and client module graphs; it is a file-level directive, placed before the imports.
  • Everything the marked file imports becomes part of the client bundle, so it only needs to go on the entry point, not on every child.
  • Client Components still render to HTML on the server; the directive adds hydration, it does not turn off server rendering.
  • Server Components passed in as children (or other props) stay on the server even inside a client parent.
  • Props that cross the boundary must be serializable; state, effects, event handlers and browser APIs are the reasons to add it.

Clarifying questions expected:

  • "Which part of the page needs interactivity?" β€” the directive belongs on that leaf, not on the layout.
  • "Is anything in this tree reading secrets or the database?" β€” that code must not end up below the boundary.

Code / implementation expected: Yes β€” a small interactive leaf with the directive, used from a server page.

  • nextjs
  • use-client
  • client-components
  • rsc

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read Β· 8 sections

Read the code

A client leaf used from a server page, with server content passed through
// components/ClientShell.tsx
"use client";
import { useState } from "react";

export default function ClientShell({ children }: { children: React.ReactNode }) {
  const [open, setOpen] = useState(true);
  return (
    <div>
      <button onClick={() => setOpen(!open)}>toggle</button>
      {open ? children : null}
    </div>
  );
}

// app/page.tsx  (Server Component: no directive)
import ClientShell from "@/components/ClientShell";
import { getReport } from "@/lib/reports"; // starts with import "server-only"

export default async function Page() {
  const report = await getReport();
  return (
    <ClientShell>
      <p>{report.summary}</p>
    </ClientShell>
  );
}

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 92 of 129 in Next.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Next.js questions
Up nextMedium

What is the output: 'standalone' build in Next.js and when do you use it?

Continue