Course map
Stage 1: App Router Foundations
Stage 2: The Rendering Model
Stage 3: Data, Caching and Mutations
Stage 4: Full-Stack Next.js
Stage 5: Production and Specialization

Part 06 / Topic 1 of 6

The "use client" directive and where the boundary actually goes

"use client" is a single string literal at the very top of a file, above every import. It does not mark "this component is now interactive" — it marks the module itself, and by extension, everything that module statically imports, as belonging to the client bundle. Part 5 section 3 already proved the mirror image of this (a file with no directive that calls useState fails the build); this part's AddToCartCounter is the fix for exactly that failure, applied on purpose:

// harness/app/store/_components/AddToCartCounter.tsx
"use client";

import { useState } from "react";

export function AddToCartCounter({ productId, name, price }: { productId: string; name: string; price: number }) {
  const [quantity, setQuantity] = useState(1);
  const [added, setAdded] = useState<number | null>(null);
  // ...
}

The question this section actually wants you to ask isn't "does this file need "use client"?" in isolation — it's "where, in a tree of components, should the one "use client" boundary for this feature go?" Apna Bazaar's quick-add page answers it by keeping the directive on the two smallest components that genuinely need browser state (AddToCartCounter's quantity, ExpandablePanel's open/closed), and nowhere else:

// harness/app/store/(shop)/quick-add/[id]/page.tsx — no "use client" here
import { findProduct, products } from "../../../lib/products";
import { AddToCartCounter } from "../../../_components/AddToCartCounter";
import { ExpandablePanel } from "../../../_components/ExpandablePanel";
import { ProductSpecs } from "../../../_components/ProductSpecs";

export default async function QuickAddPage({ params }: PageProps<"/store/quick-add/[id]">) {
  const { id } = await params;
  const product = findProduct(id);
  if (!product) notFound();

  return (
    <main>
      <h1>{product.name}</h1>
      <AddToCartCounter productId={product.id} name={product.name} price={product.price} />
      <ExpandablePanel title="product details">
        <ProductSpecs product={product} />
      </ExpandablePanel>
    </main>
  );
}

QuickAddPage stays an async Server Component — it still does the Part 5-style direct findProduct(id) lookup and the real notFound() call, neither of which would be legal in a Client Component. Section 4 reproduces, for real, exactly what breaks if this page's own boundary were drawn in the wrong place.

Full files: examples/01-add-to-cart-counter.tsx, examples/04-quick-add-page.tsx.

Done here? The practice set for Part 6 — including the solutions — is on the part page.

Search the guide