useContext Returns the Current Value
What you'll learn: How useContext gives you the exact value stored in the nearest Provider at the moment your component renders.
The Core Concept
When you call useContext(MyContext) inside a component, React looks up the component tree to find the nearest <MyContext.Provider>. Whatever value that Provider has in its value prop right now is what useContext returns to you.
This is a snapshot at render time. Just like useState gives you the current state value, useContext gives you the current context value from the Provider above.
How It Works
Think of context like a radio station broadcasting a signal. The Provider is the radio tower sending out a value. When your component calls useContext, it's like tuning into that station—you receive whatever is currently being broadcast.
const ThemeContext = createContext();
function App() {
return (
<ThemeContext.Provider value="dark">
<Toolbar />
</ThemeContext.Provider>
);
}
function Toolbar() {
// useContext returns "dark" — the current value from the Provider above
const theme = useContext(ThemeContext);
console.log(theme); // "dark"
return <div>Current theme: {theme}</div>;
}
What "Current" Means
The value returned is always synchronized with the Provider. If the Provider's value prop changes (perhaps because a parent component re-rendered with new state), components using useContext automatically re-render with the new value.
You don't need to manually subscribe or unsubscribe—React handles the connection for you.
Key Takeaway: useContext returns the live value from the nearest Provider above in the tree, giving you a current snapshot every time your component renders.