Custom Hooks — Complete Guide
Custom Hooks — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of React.js Tutorial on Toolliyo Academy.
On this page
React.js Tutorial · Lesson 30 of 100
Compound Components
Beginner ✓ → Intermediate → Advanced → Professional
Intermediate · 2 — Building apps · ~14 min read · Module 3: Hooks & Component Design
Introduction
You know the basics now. Here we use Compound Components in real app situations — forms, pages, and data. Still plain language, just a bit more depth. Compound components are a set of components that work together — like
Hooks look strange at first. Every React developer felt that way. Use the same pattern from this lesson again and again until it feels normal.
When will you use this?
Reach for hooks whenever the screen must react to user input, time, or data from an API.
- Hooks power live search, dark mode toggles, and fetching data when a page opens.
- Almost every React job posting mentions hooks — this is core daily work.
Real-world: Practo tabbed patient record
Patient chart has tabs: Vitals, Prescriptions, Labs. Compound Tabs component lets clinic devs rearrange order without prop explosion on one mega-component.
Production-style code
const TabsContext = createContext(null);
function Tabs({ children, defaultIndex = 0 }) {
const [active, setActive] = useState(defaultIndex);
return (
<TabsContext.Provider value={{ active, setActive }}>
<div className="tabs">{children}</div>
</TabsContext.Provider>
);
}
Tabs.List = function TabList({ children }) { return <div role="tablist">{children}</div>; };
Tabs.Tab = function Tab({ index, children }) {
const { active, setActive } = useContext(TabsContext);
return (
<button role="tab" aria-selected={active === index} onClick={() => setActive(index)}>
{children}
</button>
);
};
Tabs.Panel = function Panel({ index, children }) {
const { active } = useContext(TabsContext);
return active === index ? <div role="tabpanel">{children}</div> : null;
};
What happens in production: Radix, Reach, and in-house design systems use compound components — flexible API familiar to senior React developers.
Lesson example (start here)
Copy this smaller example first. Once it works, compare it with the real-world code above.
function Tabs({ children }) {
const [active, setActive] = useState(0);
return (
<TabsContext.Provider value={{ active, setActive }}>
{children}
</TabsContext.Provider>
);
}
Tabs.List = TabList;
Tabs.Panel = TabPanel;
Line-by-line walkthrough
| Code | What it means |
|---|---|
function Tabs({ children }) { | Defines a function — often a component or event handler. |
const [active, setActive] = useState(0); | Creates state: a value that can change, plus a function to update it. |
return ( | Sends UI or a value back to whoever called this function. |
<TabsContext.Provider value={{ active, setActive }}> | JSX tag — a UI element or custom component on the page. |
{children} | Part of the Compound Components example — read it together with the lines before and after. |
</TabsContext.Provider> | Part of the Compound Components example — read it together with the lines before and after. |
); | Closes a block started by { or ( above. |
} | Closes a block started by { or ( above. |
Tabs.List = TabList; | Part of the Compound Components example — read it together with the lines before and after. |
Tabs.Panel = TabPanel; | Part of the Compound Components example — read it together with the lines before and after. |
How it works (big picture)
- Parent holds active tab index.
- List and Panel read context.
- Public API feels like HTML: structure in JSX, logic hidden inside.
Do this on your computer
- Study how native
- Use Context to share tab index between subcomponents.
- Export subcomponents as properties on the parent.
- Read the real-world section and name which part of the app uses this topic.
- Run the example locally and confirm the same behavior in the browser.
- Change one value in the example (text, initial state, or URL) and predict what will happen before you save.
Experiments — try changing this
- Change text or labels in the example and save — watch the browser update.
- Break the code on purpose (remove a bracket), read the error message, then fix it.
- Change the initial state value and see the starting UI change.
- Open React DevTools (browser extension) while running Compound Components and inspect component props/state.
Remember
Compound components share state internally. Good for tabs, accordions, menus. Use Context to connect pieces.
Common questions
Compound vs single component with props?
Compound when layout flexibility matters; props when API is small.
How long should I spend on Compound Components?
Until you can explain it in your own words and run the example without looking at the answer. Beginners often need 30–60 minutes per new hook or routing topic; setup lessons may take one afternoon.
What if I get stuck on Compound Components?
Re-read the line-by-line walkthrough, check the browser console for red errors, and compare your code character-by-character with the example. Search the exact error text — someone else had it too.
Where is Compound Components used in real jobs?
See the real-world section above — the same pattern appears in LMS, banking, e-commerce, and SaaS products. Interviewers ask you to explain it using one concrete example from your project or this lesson.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!