Lesson 7 of 80
How a value gets read by any descendant without threading it through every component in between — and the re-render cost that comes with it.
13 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 4
What's the closest vanilla-JS equivalent to "a value read via Context, without being explicitly passed down"?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
From the props lesson: prop drilling means passing a value through components that don't use it, just so a descendant further down can. Context is React's answer — a value provided once, high in the tree, that any descendant can read directly with useContext, skipping every layer in between.
const ThemeContext = createContext(null);
function App() {
return (
<ThemeContext.Provider value={{ color: "dark" }}>
<Toolbar />
</ThemeContext.Provider>
);
}
function Toolbar() {
return <Button />; // doesn't need to know about theme at all
}
function Button() {
const theme = useContext(ThemeContext);
return <button className={theme.color}>Click</button>;
}
Toolbar never touches theme — it doesn't even know ThemeContext exists. Button, several layers down, reads it directly.
<ThemeContext.Provider value={{ color: 'dark' }}> wraps the tree, high up.
<ThemeContext.Provider value={{ color: "dark", updatedAt: Date.now() }}>
If this value is a fresh object literal built inline on every render of the Provider, every single component that calls useContext(ThemeContext) re-renders on every one of those renders — even a component that only ever reads theme.color and doesn't care about updatedAt at all. Context subscribes to changes in the value's reference, not to which specific field a consumer actually reads.
ThemeContext and UserContext instead of one combined AppContext) — a component reading only UserContext won't re-render when just the theme changes.useContext for that context re-render because of a value change — everything else renders (or doesn't) exactly as it normally would.value on every render defeats any memoization downstream, the same referential-equality issue from the props lesson — useMemo the value object if it's expensive to recompute or has many consumers.useState or a reducer living above the Provider."What problem does Context actually solve, and what does it cost?" checks that you see it as a targeted fix for prop drilling with a specific re-render tradeoff — not a free, magic global-state feature, which is the most common misconception about it.