How does useContext work in React?
IntermediateReact interview questionBy UIRound, updated 17 November 2026
Short answer
useContext(SomeContext) returns the value from the nearest provider of that context above the component in the tree. You create the context with createContext(defaultValue), wrap part of the tree in a provider with a value, and any component inside can read it without props being passed through every level. When the provider's value changes, every component that reads it re-renders.
The three pieces
import { createContext, useContext, useState } from "react";
// 1. Create the context, with a default value
const ThemeContext = createContext("light");
// 2. Provide a value to a subtree
function App() {
const [theme, setTheme] = useState("dark");
return (
<ThemeContext value={theme}>
<Toolbar />
<button onClick={() => setTheme(theme === "dark" ? "light" : "dark")}>
Toggle theme
</button>
</ThemeContext>
);
}
// 3. Read it anywhere below
function Toolbar() {
return <ThemedButton />;
}
function ThemedButton() {
const theme = useContext(ThemeContext);
return <button className={`btn btn-${theme}`}>Save</button>;
}Toolbar never mentions the theme. ThemedButton reads it directly, which is how context fixes prop drilling.
In React 19 you render the context itself as the provider: <ThemeContext value={theme}>. Older code writes <ThemeContext.Provider value={theme}>, which still works in React 19, but the React team plans to deprecate it.
How React finds the value
When a component calls useContext, React walks up the tree from that component to the closest provider for that exact context object, and uses its value. Providers can be nested; the inner one wins for everything inside it. This lets you override a value for one section, for example a dark sidebar inside a light page.
The lookup is by the React tree, not the DOM tree. A component rendered through a portal still reads the providers of the component that rendered the portal.
The default value
The argument to createContext is used only when there is no provider above the component at all. It is not a fallback for value={undefined}: if a provider passes undefined, the consumer gets undefined.
A common pattern is to use null as the default and throw a helpful error in a custom hook, so a missing provider fails loudly instead of silently using a fake value:
const AuthContext = createContext(null);
export function useAuth() {
const auth = useContext(AuthContext);
if (auth === null) throw new Error("useAuth must be used inside <AuthProvider>");
return auth;
}When consumers re-render
When a provider re-renders with a new value, React compares it with the previous one using Object.is. If it changed, every component that reads that context re-renders, even if a parent in between is wrapped in memo. Context skips past memoised components to reach its consumers.
Two consequences follow.
An object literal value changes on every render. value={{ user, setUser }} creates a new object each time the provider renders, so every consumer re-renders even when user is the same. Memoise it:
function AuthProvider({ children }) {
const [user, setUser] = useState(null);
const value = useMemo(() => ({ user, setUser }), [user]);
return <AuthContext value={value}>{children}</AuthContext>;
}Passing children through matters too. The children elements are created by the provider's parent, so when only AuthProvider's state changes, React does not re-render them unless they read the context.
A consumer cannot subscribe to part of a value. A component that only needs setUser still re-renders when user changes. The fix is to split the context:
| Approach | Effect |
|---|---|
One context with { state, dispatch } |
Every consumer re-renders on every state change |
StateContext and DispatchContext |
Components that only dispatch never re-render; dispatch is stable |
| Separate contexts per concern (theme, auth, locale) | A theme change does not touch auth consumers |
| A store with selectors | Each component re-renders only when its slice changes |
If you find yourself splitting context many ways to control re-renders of fast-changing state, a store is usually the better tool. Context API vs Redux covers that choice, and How React rendering works explains the re-render rules in depth.
use(Context) in React 19
React 19 adds use, which can read a context like useContext does. The difference is that use may be called inside conditions and loops, so you can read a context only after an early check. useContext must follow the rules of hooks.
Practise it
Build theme and settings providers covers helpful errors, nested overrides and stable values. Stop a context from re-rendering every consumer and Build useContextSelector tackle the re-render cost. Call use conditionally and Predict which React components re-render check the details.
Common follow-up questions
Is context a state management tool?
No. Context only transports a value down the tree. The state still lives in useState or useReducer in the provider. Context plus a reducer gives you simple global state, without selectors or devtools.
Can a child update the context value?
Yes, if the provider passes a setter or dispatch as part of the value. The child calls it, the provider's state changes, and the new value flows down.
Can you use context in Server Components?
No. createContext and useContext only work in Client Components. In Next.js, put the provider in a "use client" file and render it from a server layout; the server children you pass through still render on the server.
What happens if two copies of the same context are created?
They are different contexts. A provider for one does not satisfy useContext for the other. This happens when a module is bundled twice or a context is created inside a component, so create contexts once at module level.
Mistakes to avoid
Passing a new object or array literal as
valueon every renderPutting fast-changing state, such as a text input's value, in a context read by many components
Creating the context inside a component, so each render makes a new one
Relying on the default value and never noticing a missing provider
Reaching for context when passing
childrenwould remove the drilling
Sources
Practise it in code
Interviewers rarely stop at the definition. These questions open in an editor with tests.
- Build theme and settings providers: helpful hook errors, scoped overrides,
composeProvidersand stable valuesMedium35 min - Stop a context from re-rendering every consumerMedium30 min
- Build
useContextSelector: re-render a consumer only when its slice of context changesHard45 min - Call
useconditionally: read context after an access check and load comments only when neededMedium35 min - Predict which React components re-render:
memo, children props and contextMedium25 min