LearnFrenzy
Sign in Take a free test

Server and Client Components · Lesson 9 of 24

Next.js Client Components and "use client"

7 min read Updated Nov 8, 2026 Saurabh Samir

Server Components render the page; Client Components make it interactive. You opt in by writing "use client" at the top of a file. That component – and everything it imports – is then also sent to the browser, where it can use state, effects, event handlers and browser APIs. The skill is putting that boundary in the right place: as low in the tree as possible.

In this lesson

  • Adding interactivity with "use client"
  • Keeping the boundary small
  • Passing Server Components into Client Components as children
  • Serializable props and context providers

Your first Client Component

// app/like-button.tsx
"use client";                                  // this file and its imports run in the browser too

import { useState } from "react";

export function LikeButton({ initialLikes }: { initialLikes: number }) {
  const [likes, setLikes] = useState(initialLikes);
  const [liked, setLiked] = useState(false);
  return (
    <button
      onClick={() => {
        setLiked(!liked);
        setLikes((n) => n + (liked ? -1 : 1));
      }}
    >
      {liked ? "♥" : "♡"} {likes}
    </button>
  );
}
// app/page.tsx
import { LikeButton } from "./like-button";

export default async function LessonPage() {
  const lesson = { title: "Server Components", likes: 41 };   // fetched on the server
  return (
    <article>
      <h1>{lesson.title}</h1>
      <p>Lesson text rendered on the server…</p>
      <LikeButton initialLikes={lesson.likes} />          {/* the only interactive part */}
    </article>
  );
}

Page

Server Components
Lesson text rendered on the server…
[♡ 41]

› click “♡ 41”
Server Components
Lesson text rendered on the server…
[♥ 42]

› click “♥ 42”
Server Components
Lesson text rendered on the server…
[♡ 41]

The page is still a Server Component; only LikeButton ships JavaScript. Client Components are rendered to HTML on the server too (so the button text is in the initial HTML), then hydrated in the browser to attach the click handler.

The component tree

  • LessonPage (Server) – data, heading, text
    • LikeButton (Client) – state and onClick

Keep the boundary low

"use client" marks a boundary: every component imported into a client file becomes client code too. If you put it on a whole page, its data fetching, its large libraries and all its children move into the browser bundle. Instead, extract just the interactive piece – a search box, a like button, a carousel – into its own client file, as above.

You need "use client" when a component uses…

  • state or effects: useState, useReducer, useEffect
  • event handlers: onClick, onChange, onSubmit with a function
  • browser APIs: window, localStorage, geolocation
  • context (useContext) or hooks like usePathname and useRouter
  • a third-party library that uses any of the above

Server Components inside Client Components

A client file can't import a Server Component, but it can render one passed in as children (or any other prop). The server part is rendered on the server and slotted in:

// app/collapsible.tsx
"use client";

import { useState } from "react";

export function Collapsible({ title, children }: { title: string; children: React.ReactNode }) {
  const [open, setOpen] = useState(false);
  return (
    <section>
      <button onClick={() => setOpen(!open)}>{open ? "Hide" : "Show"} {title}</button>
      {open && children}
    </section>
  );
}
// app/page.tsx
import { Collapsible } from "./collapsible";

async function Transcript() {                          // a Server Component
  const text = "Welcome! In this lesson we compare Server and Client Components.";
  return <p>{text}</p>;
}

export default function Page() {
  return (
    <Collapsible title="transcript">
      <Transcript />                                   {/* server-rendered, passed as children */}
    </Collapsible>
  );
}

Page

[Show transcript]

› click “Show transcript”
[Hide transcript]
Welcome! In this lesson we compare Server and Client Components.

This pattern – an interactive shell from the client, content from the server – is how you build modals, tabs, accordions and providers without turning the content into client code.

Props must be serializable

Props passed from a Server Component to a Client Component travel over the network, so they must be serializable: strings, numbers, booleans, plain objects and arrays, Dates, Maps and Sets, JSX, and Promises. Regular functions can't be sent:

// app/rate.tsx
"use client";

export function Rate({ onRate }: { onRate: (stars: number) => void }) {
  return <button onClick={() => onRate(5)}>Rate 5 stars</button>;
}
// app/page.tsx
import { Rate } from "./rate";

export default function Page() {
  return <Rate onRate={(stars) => console.log(stars)} />;   // a function from the server
}

next build

Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
Error: Event handlers cannot be passed to Client Component props.
  {onRate: function onRate}
           ^^^^^^^^^^^^^^^
If you need interactivity, consider converting part of this to a Client Component.
  digest: '2608669311'
}
Export encountered an error on /page: /, exiting the build.
⨯ Next.js build worker exited with code: 1 and signal: null

The exception is Server Functions (marked "use server"), which can be passed to Client Components and are called over the network – they're covered in the Server Actions lesson. For purely client-side callbacks, define the function inside a Client Component.

Context providers

React context only works in Client Components, so providers live in a client file and wrap the app from the root layout:

// app/theme-provider.tsx
"use client";

import { createContext, useContext, useState } from "react";

const ThemeContext = createContext<{ theme: string; toggle: () => void } | null>(null);

export function ThemeProvider({ children }: { children: React.ReactNode }) {
  const [theme, setTheme] = useState("light");
  return (
    <ThemeContext value={{ theme, toggle: () => setTheme((t) => (t === "light" ? "dark" : "light")) }}>
      {children}
    </ThemeContext>
  );
}

export function ThemeToggle() {
  const ctx = useContext(ThemeContext);
  if (!ctx) throw new Error("ThemeToggle must be inside ThemeProvider");
  return <button onClick={ctx.toggle}>Theme: {ctx.theme}</button>;
}
// app/layout.tsx
import { ThemeProvider } from "./theme-provider";
import "./globals.css";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        <ThemeProvider>{children}</ThemeProvider>   {/* pages stay Server Components */}
      </body>
    </html>
  );
}
// app/page.tsx
import { ThemeToggle } from "./theme-provider";

export default function Home() {
  return (
    <main>
      <h1>Settings</h1>
      <ThemeToggle />
    </main>
  );
}

Page

Settings
[Theme: light]

› click “Theme: light”
Settings
[Theme: dark]

Wrapping children in a client provider doesn't make those children Client Components – the page passed in is still rendered on the server.

Practice

Try it yourself

  1. On a server-rendered product page, add a client-side quantity picker (−, +) that starts at 1.

    Show solution
    // app/quantity.tsx
    "use client";
    
    import { useState } from "react";
    
    export function Quantity() {
      const [qty, setQty] = useState(1);
      return (
        <p>
          <button onClick={() => setQty((q) => Math.max(1, q - 1))}>−</button> {qty}{" "}
          <button onClick={() => setQty((q) => q + 1)}>+</button>
        </p>
      );
    }
    // app/page.tsx
    import { Quantity } from "./quantity";
    
    export default function Product() {
      return (
        <main>
          <h1>Mechanical Keyboard – ₹3,499</h1>
          <Quantity />
        </main>
      );
    }

    Page

    Mechanical Keyboard – ₹3,499
    [−] 1 [+]
    
    › click “+”
    Mechanical Keyboard – ₹3,499
    [−] 2 [+]
    
    › click “+”
    Mechanical Keyboard – ₹3,499
    [−] 3 [+]

Quick check

Are Client Components rendered on the server too?

Yes – to HTML for the first load; then they hydrate in the browser.

How can a Client Component display a Server Component?

Receive it as children (or another prop) from a Server Component parent.

Can you pass an ordinary function from a Server to a Client Component?

No – props must be serializable. Server Functions ("use server") are the exception.

Summary

  • "use client" marks a boundary: that file and its imports also run in the browser.
  • Keep the boundary low – extract only interactive pieces; pass server content as children.
  • Props crossing the boundary must be serializable; put context providers in client files.

Finished this lesson?

Mark it complete to track your progress, then carry on with the next lesson.

Next lesson

Search LearnFrenzy

Esc

Quick links