Course contentsShow
← React
Lesson 1113 of 2,39628. Composition: Compound ComponentsFree lesson

Attaching Children to Parent Namespace

Export child components as properties of the parent (Parent.Child) for intuitive API usage.

Attaching Children to Parent Namespace

What you'll learn: How to export child components as properties of the parent component (like Tabs.Panel) to create a cleaner, more discoverable API.

The Core Idea

Once you've built your parent and child components for a compound pattern, you can attach the children directly to the parent as properties. Instead of exporting Tabs, TabsList, TabsPanel separately, you export just Tabs with the children attached as Tabs.List and Tabs.Panel. This signals to developers that these components belong together.

Why This Matters

Think of it like a toolkit where all related tools come in one case. Rather than searching through imports for scattered pieces, developers immediately see which components work together. The dot notation (Parent.Child) creates a visual hierarchy that mirrors the conceptual relationship.

How It Works

After creating your parent and child components, you attach them before exporting:

function Tabs({ children }) {
  // ... parent logic with provider
  return <TabsProvider>{children}</TabsProvider>;
}

function TabsList({ children }) {
  // ... child component logic
}

function TabsPanel({ children }) {
  // ... child component logic
}

// Attach children to parent namespace
Tabs.List = TabsList;
Tabs.Panel = TabsPanel;

export default Tabs;

Now consumers import just Tabs and access everything through it:

<Tabs>
  <Tabs.List>
    {/* ... */}
  </Tabs.List>
  <Tabs.Panel>
    {/* ... */}
  </Tabs.Panel>
</Tabs>

This pattern makes the relationship explicit, reduces import clutter, and guides developers toward correct usage.

Key Takeaway: Attaching child components as properties of the parent (Parent.Child = ChildComponent) creates an intuitive, self-documenting API that keeps related components together.