React has a really elegant pattern that many developers don’t take advantage of. Instead of controlling components with dozens of props, React allows you to compose components using smaller building blocks. This pattern is often called compound components. The idea is simple: instead of configuring a component through many props, the parent component exposes smaller components that you can combine however you want. It actually took me some time working with React to realize this pattern existed, so this post is mostly for those who are earlier in their journey. If you didn’t know about this concept, you’ve probably already seen it in component libraries like Material UI, Radix UI, etc... It’s a simple idea, but it makes components much more flexible and easier to scale. Here’s a small example. #React #JavaScript #Frontend #WebDevelopment #SoftwareEngineering
And as component grows, so does the bundle size by doing this, I recommend doing the more abstracted version and not stuff everything into “Card”, so multiple components that makes the composition of Card. So instead of Card.Header, have a new component that is CardHeader. Another thing - Only do composition when the requirements call for it, ive seen some cases where composition hides structure and introduces wierd bugs.
Card.Header or CardHeader? 🤔
Very nice pattern!🔥 I think this example is a bit simplified though — in cases like this the compound pattern might be unnecessary. It becomes much more valuable when you’re building complex UI components where many child elements would otherwise require a lot of props. I recently used this approach to build a card component. The <Card> acted as a context provider, and inside it I exposed smaller blocks like <Card.Title>, <Card.Description>, <Card.AddToCalendar>, <Card.Purchase>, etc. This made it much easier to compose different card layouts while reusing the same building blocks.
Compound components are a great pattern for API design in component libraries. They allow you to move from configuration (props) to composition, which tends to scale better as components grow. The main trade-off is bundle size when many subcomponents are exported together. Still a really powerful approach for building flexible UI primitives.
This will not work when accessed from server components
Recently I used the compound component pattern in one of my projects. It helped break a complex feature into smaller reusable chunks with different responsibilities. I created wrapper components that could be composed based on the use case, which made the implementation much more flexible.
Super fan of compound pattern in react, probably the best way to write react component.
I really like this pattern. In practice it becomes even more powerful when combined with the Context API so child components can share state without prop drilling.
Well explained… It gives consumers flexibility without exposing too many configuration props, which often keeps the API cleaner as the component grows.
Why does people think it’s a React feature, what I see it’s JS Vanilla functions that return React Nodes 😅 So even though you don’t use React you still have that functionality on JS Vanilla