JavaScript Curriculum
Context API
mediumThe coffee shop app needs theme, user data, and cart state accessible in many components at different depths. Passing these as props through every level becomes unmanageable. Context is the clean solution.
Context API
Context provides a way to share values between components without explicitly passing a prop through every level of the tree. Create a context, provide it once near the top, consume it anywhere below — no matter how deep.
createContext + Provider + useContext
The three-step pattern — create, provide, consume:
Context jumps the tree — consumers read directly from the Provider, no matter how deep they are.
Prop Drilling vs Context — The Problem and Solution
See how many components needlessly receive props before context:
// ❌ Prop drilling — user passed through every level function App() { const [user] = useState({ name: 'Jordan' }) return <Layout user={user} /> // doesn't need it } function Layout({ user }) { return <Sidebar user={user} /> // doesn't need it } function Sidebar({ user }) { return <NavMenu user={user} /> // doesn't need it } function NavMenu({ user }) { return <UserBadge user={user} /> // finally uses it! } // 4 levels of props just to get data to one component
Theme Context — Live Builder
Toggle theme and font size — all consumers update from one Provider:
{
theme: "dark",
fontSize: "md",
colors: {
bg: "#0d1117",
text: "#c9d1d9",
accent: "#1f6feb",
}
}Your Challenge
Build ThemeContext: createContext({ theme: 'dark', toggle: () => {} }). Write ThemeProvider that wraps children with the Provider, holds theme state, and passes { theme, toggle } as value. Create useTheme() custom hook: return useContext(ThemeContext). Use useTheme() in Header and MenuCard.
Challenge
Create a ThemeContext with createContext. Build a ThemeProvider that wraps the app, holds theme state, and provides theme + toggleTheme. Consume it in Header and MenuCard with useContext. No prop drilling needed.
Context API
mediumThe coffee shop app needs theme, user data, and cart state accessible in many components at different depths. Passing these as props through every level becomes unmanageable. Context is the clean solution.
Context API
Context provides a way to share values between components without explicitly passing a prop through every level of the tree. Create a context, provide it once near the top, consume it anywhere below — no matter how deep.
createContext + Provider + useContext
The three-step pattern — create, provide, consume:
Context jumps the tree — consumers read directly from the Provider, no matter how deep they are.
Prop Drilling vs Context — The Problem and Solution
See how many components needlessly receive props before context:
// ❌ Prop drilling — user passed through every level function App() { const [user] = useState({ name: 'Jordan' }) return <Layout user={user} /> // doesn't need it } function Layout({ user }) { return <Sidebar user={user} /> // doesn't need it } function Sidebar({ user }) { return <NavMenu user={user} /> // doesn't need it } function NavMenu({ user }) { return <UserBadge user={user} /> // finally uses it! } // 4 levels of props just to get data to one component
Theme Context — Live Builder
Toggle theme and font size — all consumers update from one Provider:
{
theme: "dark",
fontSize: "md",
colors: {
bg: "#0d1117",
text: "#c9d1d9",
accent: "#1f6feb",
}
}Your Challenge
Build ThemeContext: createContext({ theme: 'dark', toggle: () => {} }). Write ThemeProvider that wraps children with the Provider, holds theme state, and passes { theme, toggle } as value. Create useTheme() custom hook: return useContext(ThemeContext). Use useTheme() in Header and MenuCard.
Challenge
Create a ThemeContext with createContext. Build a ThemeProvider that wraps the app, holds theme state, and provides theme + toggleTheme. Consume it in Header and MenuCard with useContext. No prop drilling needed.