Modern Full-Stack & React Internals · Chapter 3 · ~15 min · Intermediate
Component Composition, Props Drilling & Children
Master modern component architecture: composition over inheritance, the children prop, layout containment slots, prop drilling vs context boundaries, and building flexible compound components.
- Master the children prop and compound containment patterns
- Replace rigid prop drilling with flexible component composition
- Build slot-based layouts using named component props
- Distinguish when to lift state vs when to introduce context
Component Composition, Props Drilling & Children: learn it visually
Master modern component architecture: composition over inheritance, the children prop, layout containment slots, prop drilling vs context boundaries, and building flexible compound components.
1Composition Over Inheritance in React
In object-oriented languages like Java, C++, or traditional class-based frameworks, code reuse is often achieved through inheritance: creating a base class (like BaseModal) and extending it with subclasses (ConfirmModal extends BaseModal).
In React, the official documentation states plainly: "At Facebook, React is used in thousands of components, and we haven't found any use cases where we would recommend creating component inheritance hierarchies."
Instead, React embraces Composition: components accept other components as props or nest them using the special children prop.
Wrong idea: Class inheritance trees (BaseModal extends Container)
Creating rigid class hierarchies where base components pass methods and styles down deep inheritance chains, causing tight coupling and brittle overrides.
Real system: Composition via children and slot props
Composing small, independent components by passing elements into children or named props (slots), allowing arbitrary nesting without coupling.
2Containment with the children Prop
Some components don't know their children ahead of time. This is especially common for layout boxes like Sidebar, Dialog, Card, or SplitPane.
We recommend that such components use the special children prop to pass children elements directly into their output:
import { type ReactNode } from "react";
interface FancyBoxProps {
color: "blue" | "green";
children: ReactNode;
}
export function FancyBox({ color, children }: FancyBoxProps) {
return (
<div className={`fancy-box border-${color}`}>
{/* Anything nested inside <FancyBox>...</FancyBox> lands right here: */}
{children}
</div>
);
}
// Consumer usage:
<FancyBox color="blue">
<h2 className="title">Welcome!</h2>
<p className="message">Thank you for visiting our space.</p>
</FancyBox>3Named Slots: Passing Elements as Props
While children provides one default hole, real-world layout containers frequently need multiple holes — for example, a left sidebar and a main pane, or a card with a distinct header, body, and footer.
Because React elements are plain JavaScript objects, you can pass JSX nodes into any prop, not just children:
interface SplitLayoutProps {
left: React.ReactNode;
right: React.ReactNode;
}
export function SplitLayout({ left, right }: SplitLayoutProps) {
return (
<div className="flex min-h-screen">
<aside className="w-64 border-r p-4">{left}</aside>
<main className="flex-1 p-6">{right}</main>
</div>
);
}
// Usage with named element slots:
<SplitLayout
left={<NavigationMenu links={userLinks} />}
right={<DashboardFeed query="active" />}
/>4Inversion of Control: Defeating Prop Drilling
Prop drilling occurs when you pass data through multiple layers of intermediate components just to get it to a deeply nested child:
Page(user) → Shell(user) → Header(user) → UserMenu(user) → Avatar(user)
The intermediate components (Shell and Header) don't care about user, yet they must accept and forward it. If user changes shape, every intermediate component breaks!
The Solution: Component Composition. Instead of passing the raw data down, instantiate the leaf component at the top level and pass the element down:
// At the Page level where user data lives:
function Page({ user }) {
// We compose the avatar right here:
const userAvatar = <Avatar user={user} size="lg" />;
// Shell and Header only forward the JSX element slot:
return <Shell userSlot={userAvatar} />;
}
// Header does NOT need to know what 'user' looks like:
function Header({ userSlot }: { userSlot: ReactNode }) {
return (
<header className="flex justify-between items-center">
<Logo />
{userSlot}
</header>
);
}Predict first
Why does Component Composition often eliminate the need to create a React Context?
5Hands-On: React Composition & Slot Explorer
Test how slot composition creates resilient architectures. Toggle slots, customize contents, and compare the clean modular JSX against a rigid 14-prop monolithic component:
Interactive Component Composition & Slot Explorer
Configure Card Slots & Content:
User Security Settings
Rendered via header slot// 1. Clean Composition with Children & Named Slots:
<Card
header={
<div className="flex items-center gap-2">
<ShieldCheck className="size-5 text-primary" />
<h3 className="font-heading font-semibold">User Security Settings</h3>
</div>
}
footer={
<div className="flex justify-end gap-2">
<Button variant="outline">Cancel</Button>
<Button>Save Changes</Button>
</div>
}
>
{/* The Children Slot Accepts ANY Arbitrary Component! */}
<MetricsOverview activeSessions={3} 2faEnabled={true} />
</Card>6Bug Hunt: Fix the Modal Dialog Slot Architecture
A junior developer wrote a Modal component, but junior teammates are complaining: the modal mutates incoming props, hardcodes text rather than accepting arbitrary children, and lacks flexible footer actions.
🎯 Find and fix the bugs in the code above, then press Run & verify to test your solution.
Need a hint?
- Props are strictly read-only: delete
props.title = props.title.toUpperCase();. - Replace
<p>{props.bodyText}</p>with{props.children}(or destructurechildren). - Keep footer flexible so consumers can pass actions.
7Chapter Knowledge Challenge
Verify your mastery of component composition, slots, and containment before moving to Chapter 4:
Why does the React core team recommend Composition over Class Inheritance for UI components?
Can a prop other than 'children' accept a JSX element in React (e.g. <Card header={<Header />} />)?
How does Inversion of Control with composition solve the prop drilling problem?
8Recap & Architectural Takeaways
- Composition over Inheritance: Assemble small focused components together instead of building deep class inheritance hierarchies.
- The Children Prop: Use
childrenfor generic containment components (cards, dialogs, drawers). - Named Slots: Pass JSX elements into props like
header,sidebar, orfooterto support multi-hole layouts. - Invert Control: Before reaching for Context or state management libraries, check if lifting component instantiation solves prop drilling cleanly.
Ready to conquer Effects and Re-renders in Chapter 4?