Compound Components in React: Composition Instead of Twenty Props / Nov 27, 2026
Most components start with a few props and grow one at a time. Here is a typical FAQ section after a few months of requests:
<Faq
items={faqs}
defaultOpen="delivery"
allowMultiple={false}
headingLevel={3}
iconPosition="right"
renderAnswer={(item) => <Markdown>{item.answer}</Markdown>}
showDividers
onToggle={track}
/>Every new layout needs a new prop, and every prop is another branch inside the component. The person using it can't put a link next to a question or a badge on one item without asking for yet another option.
Compound components flip that. Instead of configuring one component, you compose several small ones that share state:
<Accordion defaultValue="delivery">
<AccordionItem value="delivery">
<h3>
<AccordionTrigger>How long does delivery take?</AccordionTrigger>
</h3>
<AccordionPanel>
<p>Two to five working days within Lagos.</p>
</AccordionPanel>
</AccordionItem>
<AccordionItem value="returns">
<h3>
<AccordionTrigger>Can I return an item?</AccordionTrigger>
</h3>
<AccordionPanel>
<p>Yes, within seven days if it is unused.</p>
</AccordionPanel>
</AccordionItem>
</Accordion>(The delivery and returns answers are placeholder copy.)
The heading level, the order, what sits inside each answer: all of it is now in the hands of
whoever uses the component. HTML has always worked this way. <select> and <option>, or
<table>, <tr> and <td>, are compound components.
How it works: the parent owns the state, the parts read it
There are two pieces of shared information here. The accordion knows which item is open. Each item knows its own value and its own ID. That makes two contexts:
"use client";
import { createContext, useContext, useId, useState } from "react";
const AccordionContext = createContext(null);
const ItemContext = createContext(null);
// Every part calls this, so using a part in the wrong place fails loudly.
function useAccordionPart() {
const accordion = useContext(AccordionContext);
const item = useContext(ItemContext);
if (!accordion || !item) {
throw new Error("Accordion parts must be used inside <Accordion> and <AccordionItem>");
}
return { ...accordion, ...item };
}
export function Accordion({ defaultValue = null, children }) {
const [openValue, setOpenValue] = useState(defaultValue);
return (
<AccordionContext value={{ openValue, setOpenValue }}>
<div className="accordion">{children}</div>
</AccordionContext>
);
}
export function AccordionItem({ value, children }) {
const id = useId();
return (
<ItemContext value={{ value, id }}>
<div className="accordion-item">{children}</div>
</ItemContext>
);
}
export function AccordionTrigger({ children }) {
const { openValue, setOpenValue, value, id } = useAccordionPart();
const open = openValue === value;
return (
<button
type="button"
id={`${id}-trigger`}
aria-expanded={open}
aria-controls={`${id}-panel`}
onClick={() => setOpenValue(open ? null : value)}
>
{children}
</button>
);
}
export function AccordionPanel({ children }) {
const { openValue, value, id } = useAccordionPart();
return (
<div
id={`${id}-panel`}
role="region"
aria-labelledby={`${id}-trigger`}
hidden={openValue !== value}
>
{children}
</div>
);
}Three details:
<AccordionContext value={...}>is the React 19 way to provide a context. On React 18, write<AccordionContext.Provider value={...}>instead.useId()gives each item a stable ID that matches between the server render and the browser, so the trigger and its panel can point at each other.- The guard hook throws a clear error if someone puts an
AccordionTriggeroutside an item, instead of failing later with "cannot read properties of null".
Rendered on the server with React 19.2, the example above comes out with the first item open
and the second rendered with aria-expanded="false" and a hidden panel, which is the right
starting state for defaultValue="delivery".
Accessibility comes built in, once
An accordion has a well-known accessible pattern, and building it as compound components means you write it once and everyone who uses the component gets it:
- The trigger is a real
<button>. It works with the keyboard and with screen readers without extra code. - The button sits inside a heading, so people who move through a page by headings can find
each question. The heading level is the caller's choice, because only the page knows whether
it should be an
h2or anh3. A props-based component would need aheadingLevelprop for that. aria-expandedtells assistive technology whether the panel is open.aria-controlsandaria-labelledbylink each trigger and its panel.hiddenremoves a closed panel from the page and the accessibility tree.
Automated checks catch some mistakes, but the only real test is to use it with a keyboard and a screen reader.
The Server Components gotcha
Many tutorials attach the parts to the parent as properties, so you can write
Accordion.Item:
Accordion.Item = AccordionItem;
Accordion.Trigger = AccordionTrigger;In a Next.js App Router project, that breaks as soon as a Server Component uses it. The
accordion file starts with "use client", so what a Server Component imports is a reference
to a client component, not the function itself. Reading a property off that reference fails
with this error from React:
You cannot dot into a client module from a server component.
You can only pass the imported name through.There are two fixes, and both keep the nice syntax for whoever needs it:
Export each part by name, as in the code above. Each export is its own client reference.
Or import the whole module as a namespace where you use it:
import * as Accordion from "@/components/Accordion";
<Accordion.Root defaultValue="delivery">
<Accordion.Item value="delivery">...</Accordion.Item>
</Accordion.Root>For that, export the parts as Root, Item, Trigger and Panel. A namespace import is
not a property on a component, so it works on the server. Radix uses this style, for example
Dialog.Root, Dialog.Trigger and Dialog.Content.
There is a bonus to keeping the parts as client components and the usage as a Server Component: the FAQ text itself is rendered on the server and arrives as HTML. Only the small toggle logic ships as JavaScript.
Let the caller control it when they need to
The version above manages its own state, which covers most uses. Sometimes the page needs control, for example to open an answer from a link elsewhere. The usual pattern is an optional pair of props on the parent:
export function Accordion({ value, onValueChange, defaultValue = null, children }) {
const [internal, setInternal] = useState(defaultValue);
const controlled = value !== undefined;
const openValue = controlled ? value : internal;
const setOpenValue = (next) => {
if (!controlled) setInternal(next);
onValueChange?.(next);
};
return (
<AccordionContext value={{ openValue, setOpenValue }}>
<div className="accordion">{children}</div>
</AccordionContext>
);
}Pass value and onValueChange and the page is in charge. Leave them out and the accordion
looks after itself. The parts don't change at all, which is the point of the pattern.
When not to bother
Compound components are worth it when a component has several distinct parts and the people using it need control over the layout: accordions, tabs, menus, dialogs, forms with sections.
For a button, a badge or an avatar, plain props are clearer. If a component has three props and nobody is asking for a fourth, leave it alone.
The short version
- When a component keeps growing props, split it into parts that share state through context.
- Build the accessibility into the parts once, and let the caller choose things like the heading level.
- In the Next.js App Router, export the parts by name or use a namespace import. Don't hang them off the parent as properties.
- Offer
valueandonValueChangefor the cases where the page needs control.
Further reading: the WAI-ARIA accordion pattern and React's docs on passing data deeply with context.
Frequently asked questions
- What are compound components in React?
- A set of components that work together and share state through context, such as Accordion, AccordionItem, AccordionTrigger and AccordionPanel. The parent owns the state and the children read it, so the person using them arranges the pieces instead of configuring one component with many props.
- Why does Accordion.Item fail in a Next.js Server Component?
- A Server Component cannot read a property off a component imported from a client module. React throws: You cannot dot into a client module from a server component. Export each part by name, or import the module as a namespace, so every part is its own client reference.
- When should I not use compound components?
- For small, single-purpose components such as a button, a badge or an avatar. Plain props are clearer there. Compound components earn their place when a component has several distinct parts and people need control over the layout.