Next.jsHard
What is the "children pattern" for mixing server and client components?
The children pattern lets a Client Component wrap server-rendered content without turning it into client code. A Server Component parent renders <ClientWrapper><ServerContent /></ClientWrapper>, and the client wrapper simply renders {children}. The server content is rendered on the server and arrives as finished output.
Why it matters
Many interactive shells need to wrap large amounts of static or data-heavy content: tabs, accordions, modals, sidebars, theme or auth providers, animation wrappers. Without the pattern, making the shell interactive would drag all of its content into the client bundle.
How it works
- The Server Component creates both elements.
- React renders
<ServerContent />on the server into the RSC payload. ClientWrapperreceiveschildrenas an opaque, already-rendered node.- The wrapper can show, hide, reorder or animate it, but cannot pass new props into it.
Variations
- Named slots:
<Layout header={<Header />} sidebar={<Nav />}>. - Providers:
<ThemeProvider>{children}</ThemeProvider>in the root layout keeps pages as Server Components. - Cache interleaving: a
"use cache"component can acceptchildrenwithout them becoming part of its cache key, as long as it does not inspect them.
Limits
- The client wrapper cannot pass props to children; use context for that, read by Client Components inside.
- Children are rendered once per server render; client state changes do not re-run server code.
- Hidden children (an inactive tab) are still rendered on the server, so very expensive content can be deferred with
<Suspense>or a route.
Code example
tsx
1// app/docs/page.tsx (Server Component)2import { Accordion } from "./accordion"; // "use client"3import { db } from "@/lib/db";45async function Faq({ id }: { id: string }) {6 const item = await db.faq.findUnique({ where: { id } }); // server-only work7 return <div dangerouslySetInnerHTML={{ __html: item!.answerHtml }} />;8}910export default function DocsPage() {11 return (12 <>13 <Accordion title="How do refunds work?">14 <Faq id="refunds" />15 </Accordion>16 <Accordion title="Can I change plans?">17 <Faq id="plans" />18 </Accordion>19 </>20 );21}2223// app/docs/accordion.tsx24// "use client";25// import { useState } from "react";26// export function Accordion({ title, children }: { title: string; children: React.ReactNode }) {27// const [open, setOpen] = useState(false);28// return (29// <section>30// <button aria-expanded={open} onClick={() => setOpen(!open)}>{title}</button>31// {open && children}32// </section>33// );34// }
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.