Compound Components
Related components share state through context.
Example
const TabsCtx = createContext(null);
function Tabs({ children }) {
const [i, setI] = useState(0);
return <TabsCtx.Provider value={{ i, setI }}>{children}</TabsCtx.Provider>;
}
function Tab({ n, children }) {
const { setI } = useContext(TabsCtx);
return <button onClick={() => setI(n)}>{children}</button>;
}Exercise
Share via context.
Tabs and Tab share Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.