Skip to content

Too Many Props, It Hurts ๐Ÿ˜“

Components that grow a new prop for every variation become hard to use and harder to maintain. Compound components are a cleaner way to build them.

NevilleFrontend engineer
ยท 4 min read

When you start a new project, adding a prop to control a particular behaviour or style of a component seems like the obvious thing to do. It works at first, but as the project grows it gets harder and harder to keep track of what each prop controls. The component becomes more complex than it needs to be, and harder to maintain in the long run.

If you've worked with React for a while, you've likely met components like this, and they're no fun either for the developers using them or for the ones maintaining them.

The problem

Let's say you've been given a design and plan to build a reusable button component that looks like this:

A gold "Add more" button with a plus icon on the left
The design: some text with an icon on the left.

The button has just some text and an icon on the left:

Button.tsx
interface ButtonProps {
  text: string;
  icon?: string;
}
 
function Button({ text, icon }: ButtonProps) {
  return (
    <button>
      {icon && <Icon name={icon} size={10} />}
      <span>{text}</span>
    </button>
  );
}
 
// Used like this:
<Button text="Add more" icon="plus" />;

This is quite straightforward!

A week later, the designer updates the component. It now needs an optional icon on the right as well as on the left, and each icon's size should be adjustable.

A gold "Add more" button with a plus icon on each side
A week later: icons on both sides, each with its own size.

That's a lot of changes ๐Ÿ˜“, so we add extra props to handle the new cases:

Button.tsxbefore
interface ButtonProps {
  text: string;
  leftIcon?: string;
  rightIcon?: string;
  leftIconSize?: number;
  rightIconSize?: number;
}
 
function Button({
  text,
  leftIcon,
  rightIcon,
  leftIconSize,
  rightIconSize,
}: ButtonProps) {
  return (
    <button>
      {leftIcon && <Icon name={leftIcon} size={leftIconSize} />}
      <span>{text}</span>
      {rightIcon && <Icon name={rightIcon} size={rightIconSize} />}
    </button>
  );
}
 
// Used like this:
<Button
  text="Add more"
  leftIcon="plus"
  rightIcon="plus"
  leftIconSize={10}
  rightIconSize={15}
/>;

We've gone from 2 props to 5, and the number can only grow: a loading spinner, a badge, a different icon colourโ€ฆ ๐Ÿคฆ๐Ÿพโ€โ™‚๏ธ Every new requirement adds props, conditions and combinations to test.

A better way: compound components ๐Ÿ’ช๐Ÿพ

Compound components are a composition pattern where you define the individual parts of a component as separate components that work together as one unit. Whoever uses the component decides which parts to include and in what order, instead of switching them on and off with props.

To understand this, think of the HTML <select> and <option> elements and how they work together.

Let's rebuild the button using compound components:

Button.tsxafter
import type { ComponentProps } from "react";
 
function ButtonRoot(props: ComponentProps<"button">) {
  return <button {...props} />;
}
 
function ButtonIcon(props: ComponentProps<typeof Icon>) {
  return <Icon {...props} />;
}
 
function ButtonText(props: ComponentProps<"span">) {
  return <span {...props} />;
}
 
export const Button = {
  Root: ButtonRoot,
  Icon: ButtonIcon,
  Text: ButtonText,
};

It can then be used like this:

AddMore.tsx
<Button.Root>
  {/* Left icon */}
  <Button.Icon name="plus" size={10} />
  <Button.Text>Add more</Button.Text>
  {/* Right icon */}
  <Button.Icon name="plus" size={15} />
</Button.Root>

Need only the left icon? Leave the right one out. Need a badge next week? Add a Button.Badge part, and none of the existing buttons change. This approach gives you a cleaner structure that's much more readable, maintainable and scalable.

When the parts need to share state

Our button's parts don't need to know about each other. Many components aren't that simple: in a dropdown, an accordion or a "show more" panel, one part toggles something and another part reacts to it. They share state.

The props-only version of such a component goes the same way as our button. Take a disclosure, a button that shows and hides a panel:

Disclosure.tsxbefore
<Disclosure
  title="Shipping details"
  content={<ShippingInfo />}
  defaultOpen={false}
  icon="chevron"
  iconPosition="right"
  titleClassName="font-bold"
  onOpenChange={trackOpen}
/>

Every new requirement (an icon on the left, a badge in the title, the panel somewhere else on the page) means another prop.

With compound components, the root owns the state and shares it through context. Each part reads just what it needs:

The root owns the open state; the trigger changes it and the panel reads it.
Disclosure.tsxafter
import {
  createContext,
  use,
  useId,
  useState,
  type ComponentProps,
  type ReactNode,
} from "react";
 
type DisclosureState = {
  open: boolean;
  toggle: () => void;
  panelId: string;
};
 
const DisclosureContext = createContext<DisclosureState | null>(null);
 
// Every part reads the shared state through this hook. It fails loudly if a part
// is used outside <Disclosure.Root>, instead of silently doing nothing.
function useDisclosure() {
  const state = use(DisclosureContext);
  if (!state) {
    throw new Error("Disclosure parts must be inside <Disclosure.Root>.");
  }
  return state;
}
 
function DisclosureRoot({
  defaultOpen = false,
  children,
}: {
  defaultOpen?: boolean;
  children: ReactNode;
}) {
  const [open, setOpen] = useState(defaultOpen);
  const panelId = useId();
  const toggle = () => setOpen((o) => !o);
 
  return (
    <DisclosureContext value={{ open, toggle, panelId }}>
      {children}
    </DisclosureContext>
  );
}
 
function DisclosureTrigger(props: ComponentProps<"button">) {
  const { open, toggle, panelId } = useDisclosure();
  return (
    <button
      type="button"
      aria-expanded={open}
      aria-controls={panelId}
      onClick={toggle}
      {...props}
    />
  );
}
 
function DisclosurePanel(props: ComponentProps<"div">) {
  const { open, panelId } = useDisclosure();
  return <div id={panelId} hidden={!open} {...props} />;
}
 
export const Disclosure = {
  Root: DisclosureRoot,
  Trigger: DisclosureTrigger,
  Panel: DisclosurePanel,
};

Using it:

Checkout.tsx
<Disclosure.Root>
  <Disclosure.Trigger>
    Shipping details <ChevronIcon />
  </Disclosure.Trigger>
  <Disclosure.Panel>
    <ShippingInfo />
  </Disclosure.Panel>
</Disclosure.Root>

Notice what disappeared: title, content, icon, iconPosition and titleClassName are gone, because you write the trigger's content and the panel's content yourself, as JSX, with whatever markup and styling you need. The trigger and the panel never receive the state as props, either. They don't even have to be next to each other: put the panel at the bottom of a card and the trigger in its header, and as long as both are inside Disclosure.Root, they stay in sync.

The parts also take care of the details everyone forgets: aria-expanded tells screen readers whether the panel is open, aria-controls links the button to the panel it controls, and useId gives every disclosure on the page its own ID.

This is how libraries such as Radix UI (opens in a new tab) and Headless UI (opens in a new tab) build their components: <Accordion.Item>, <Dialog.Trigger>, <Tabs.Content> and so on are all parts sharing state through a root.

You can learn more about this pattern on patterns.dev (opens in a new tab).

Good luck! ๐Ÿ‘‹๐Ÿป

Written by Neville

Geologist turned frontend engineer from Cameroon, based in Ghana. Makes web and mobile apps faster and writes down how. More about me

Liked this? Get the next one.

One deep dive a month. No spam, unsubscribe in one click.