0
Challenge · 29Hooks & Effects
Context API
Coreused in ~90% of apps
When a value like the current user or theme is needed ten components deep, passing it as a prop through every layer in between is misery — and every one of those middle components now lists a prop it never uses. Context is React's built-in pipe that delivers that value straight to whoever reads it.
Core idea. Context lets a parent broadcast a value to any component below it in the tree, so deep consumers read it directly with useContext instead of having it drilled through every intermediate prop. Three steps: createContext, wrap with a Provider, read with useContext.
poke it, drag it,
try to break it
try to break it
no slides here.
you drive.
you drive.
Playground, this isn't a screenshot
Set user:
<App />
owns the user
user="Aria"
<Layout />
doesn't use user
<Sidebar />
doesn't use user
<Avatar />needs user…
?
waiting for user…
App owns the user. Press a name and watch how it reaches the Avatar deep in the tree.
forced to forward (drilling) reads directly (context)
↑ play a little first, then dig in