EasyComponent Patterns📖 Theory Question

What is the Compound Component pattern?

💡

Hint

Related components share implicit state via context — like <select> and <option> in HTML

Full Answer

Compound components work together, sharing state implicitly through context. The parent manages state; children access it without explicit prop passing.

const TabsContext = createContext(null);

function Tabs({ children, defaultTab }) {
  const [active, setActive] = useState(defaultTab);
  return (
    
      
{children}
); } function TabList({ children }) { return
{children}
; } function Tab({ id, children }) { const { active, setActive } = useContext(TabsContext); return ( ); } function TabPanel({ id, children }) { const { active } = useContext(TabsContext); return active === id ?
{children}
: null; } // Usage — clean, flexible, no prop drilling Profile Settings
💡 Good examples: Tabs, Accordion, Dropdown, Form with Field/Label/Error subcomponents. The key insight — the developer using the component controls the structure; the component controls the behavior.

More Component Patterns Questions

EasyWhat is the Render Props pattern and when would you still use it?MediumWhat is the Provider pattern and how do you build a scalable context architecture?MediumWhat is the Container/Presentational pattern? Is it still relevant?

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint