MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Compound Components Pattern Explained0%
Study Materials
React.js for IntermediateProgress 0%
1.1Component Reusability and Polymorphic Components1.2Render Props Pattern in Modern React1.3Higher-Order Components vs Modern Hooks1.4Compound Components Pattern Explained
HomeTutorialsReact.js for IntermediateCompound Components Pattern Explained
Race Conditions and Stale Closures in useEffect

Compound Components Pattern Explained

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

1. What Are Compound Components?

In native HTML, certain elements work together as a synchronized family to provide an intuitive, declarative interface. The classic example is <select> and <option>:

1<select name="course">
2 <option value="react">React Mastery</option>
3 <option value="django">Django Architecture</option>
4</select>

Notice how:

  • The <select> manages the selected state and keyboard navigation.
  • The <option> tags configure individual items.
  • You don't have to pass an awkward configuration object like <select options={[{label: 'React', value: 'react'}]} />.
  • You can freely rearrange the options, insert dividing <hr /> elements, or add custom styling between options without breaking functionality.

In React, the Compound Components Pattern replicates this declarative, flexible relationship for custom UI primitives (such as Tabs, Accordions, Dropdown Menus, and Radio Groups).

2. Implementing Compound Components with React Context

To build compound components, a parent component creates a React Context to share state and updater functions with its child components implicitly.

Let's build a compound Tabs component:

1import React, { useState, createContext, useContext } from 'react';
2 
3// 1. Create Context for Tab State
4const TabsContext = createContext(null);
5 
6// 2. Parent Compound Component
7export function Tabs({ defaultActiveTab, children }) {
8 const [activeTab, setActiveTab] = useState(defaultActiveTab);
9 
10 return (
11 <TabsContext.Provider value={{ activeTab, setActiveTab }}>
12 <div className="tabs-container">{children}</div>
13 </TabsContext.Provider>
14 );
15}
16 
17// 3. Child Component: Tab Button List
18export function TabList({ children }) {
19 return <div className="tab-buttons-row" role="tablist">{children}</div>;
20}
21 
22// 4. Child Component: Individual Tab Trigger
23export function Tab({ id, children }) {
24 const { activeTab, setActiveTab } = useContext(TabsContext);
25 const isActive = activeTab === id;
26 
27 return (
28 <button
29 role="tab"
30 aria-selected={isActive}
31 className={`tab-btn ${isActive ? 'tab-btn-active' : ''}`}
32 onClick={() => setActiveTab(id)}
33 >
34 {children}
35 </button>
36 );
37}
38 
39// 5. Child Component: Tab Content Panel
40export function TabPanel({ id, children }) {
41 const { activeTab } = useContext(TabsContext);
42 if (activeTab !== id) return null;
43 
44 return (
45 <div role="tabpanel" className="tab-content-panel">
46 {children}
47 </div>
48 );
49}
50 
51// 6. Optional: Attach children as static properties for clean namespace syntax
52Tabs.List = TabList;
53Tabs.Tab = Tab;
54Tabs.Panel = TabPanel;

3. Beautiful, Declarative Consumer Experience

Consumers of compound components enjoy an elegant, self-describing API:

1export default function CourseCurriculum() {
2 return (
3 <Tabs defaultActiveTab="frontend">
4 <Tabs.List>
5 <Tabs.Tab id="frontend">Frontend Track</Tabs.Tab>
6 <Tabs.Tab id="backend">Backend Track</Tabs.Tab>
7 <Tabs.Tab id="data">Data Science</Tabs.Tab>
8 </Tabs.List>
9 
10 <Tabs.Panel id="frontend">
11 <h3>HTML5, Tailwind CSS, Modern React & Vite</h3>
12 <p>Comprehensive client-side engineering mastery.</p>
13 </Tabs.Panel>
14 
15 <Tabs.Panel id="backend">
16 <h3>Python, Django REST Framework, PostgreSQL</h3>
17 <p>Scalable backend architectures and microservices.</p>
18 </Tabs.Panel>
19 
20 <Tabs.Panel id="data">
21 <h3>Pandas, NumPy, Power BI, Statistics</h3>
22 <p>End-to-end data pipelines and business intelligence.</p>
23 </Tabs.Panel>
24 </Tabs>
25 );
26}

4. Key Architectural Benefits

  • Implicit State Sharing: The consumer never has to manually wire activeTab or onClick to each individual button; the context handles it automatically.
  • Extreme Layout Flexibility: The consumer can reorder tabs, nest tabs inside custom grid wrappers, or insert header graphics between elements without breaking state sync.
  • Inversion of Control: The consumer dictates the visual hierarchy and layout, while the compound components manage interaction logic and accessibility.

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What is the primary inspiration behind the Compound Components pattern in React?
Question 2
What React feature is typically used under the hood to share state between compound parent and child components?
Question 3
Why do libraries assign sub-components as properties on the parent (e.g. Tabs.Tab = Tab)?
Question 4
How does the Compound Components pattern improve upon rigid "prop-heavy" components like <Tabs items={dataArray} />?
Question 5
What is rendered by <Tabs.Panel id="backend"> if the active tab is "frontend"?
Next Lesson

Race Conditions and Stale Closures in useEffect

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Higher-Order Components vs Modern HooksRace Conditions and Stale Closures in useEffect

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master React.js for Intermediate with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext