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

Creating the Parent Component Shell

Set up the parent component that will orchestrate state and provide context to children.

Creating the Parent Component Shell

What you'll learn: How to build the foundational parent component that will hold shared state and orchestrate communication between compound component children.

Now that you've planned your compound component API, it's time to build the parent component that serves as the "coordinator" for all the child components.

The Parent's Role

Think of the parent component as a conductor of an orchestra. It doesn't play all the instruments itself, but it maintains the tempo (state), sets the mood (context), and ensures everyone plays in harmony. The parent creates a context provider that wraps its children, making shared state available to any child that needs it.

Basic Structure

Your parent component shell typically includes:

  1. State management — using hooks like useState to track shared data
  2. Context provider — wrapping children to share state downward
  3. Minimal rendering — the parent usually just renders its children without adding extra markup
function Tabs({ children, defaultValue }) {
  const [activeTab, setActiveTab] = useState(defaultValue);

  const contextValue = {
    activeTab,
    setActiveTab
  };

  return (
    <TabsContext.Provider value={contextValue}>
      {children}
    </TabsContext.Provider>
  );
}

The parent doesn't dictate which children render or how they look—it simply holds the shared state and makes it available. This gives users of your component full flexibility in composition.

Key Considerations

Keep the parent lightweight. It shouldn't contain complex logic or styling—those belong in the child components. The parent's only job is to create the shared environment.

Key Takeaway: The parent component shell establishes shared state, wraps children in a context provider, and stays minimal—it orchestrates without dictating structure.