Context Consumer
Predict Output Medium
Question
What does this code print?
Code
const Ctx = createContext("default");
function App() {
return <Ctx.Provider value="hi">
<Child />
</Ctx.Provider>;
}
function Child() {
const v = useContext(Ctx);
return <p>{v}</p>;
}
// What renders?
Hint
useContext reads the nearest Provider value.
Explanation
The Provider wraps Child with value="hi". useContext(Ctx) in Child reads "hi" from the nearest Provider. The paragraph renders "hi".
Related Resources
Related Lessons
- useContext
Learn useContext.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.