Full answer
Passing data through many layers of components only to reach one deep child is called prop drilling. Context solves it. You create a context, wrap part of the tree in its provider, and any component below can read the value.
The steps are short. Create the context with createContext(defaultValue). Render it above the components that need it, with a value prop. In React 19 the context itself is the provider: <AuthContext value={...}>. Older code uses <AuthContext.Provider value={...}>, which still works. Read the value with useContext(AuthContext). A common pattern is to hide this in a custom hook that also throws a clear error if the provider is missing.
The main limit is re-rendering. When the value changes (compared with Object.is), every component that reads that context renders again, even if it uses only one field. This is why the value object should be memoized when the provider re-renders for other reasons, and why separate contexts for unrelated data are better than one large context. A fast-changing value, such as mouse position or a text field, does not belong in context.
Context is also not a full state manager. It transports a value. The state itself still comes from useState or useReducer, usually inside the provider. For complex state with selectors or devtools, a library such as Redux Toolkit or Zustand fits better. For server data, TanStack Query is better than putting fetched data in context.
Defaults matter. The default value in createContext is used only when no provider is above. Using null as the default and checking in the hook makes missing providers an obvious error instead of silent wrong data.
import { createContext, useContext, useMemo, useState } from 'react';
const UserContext = createContext(null);
function UserProvider({ children }) {
const [user, setUser] = useState({ name: 'Asha' });
const value = useMemo(() => ({ user, logout: () => setUser(null) }), [user]);
return <UserContext value={value}>{children}</UserContext>;
}
function useUser() {
const ctx = useContext(UserContext);
if (ctx === null) {
throw new Error('useUser must be used inside UserProvider');
}
return ctx;
}
function Header() {
const { user, logout } = useUser();
return user ? (
<p>
Signed in as {user.name} <button onClick={logout}>Log out</button>
</p>
) : (
<p>Signed out</p>
);
}
export default function App() {
return (
<UserProvider>
<Header />
</UserProvider>
);
}Header sits below the provider and reads the user without any props. The check clicked Log out once. Result: <p>Signed out</p>
Key points
- Context passes data through the tree without props at each level.
- createContext, a provider with value, and useContext make up the whole API. In React 19 the context object works as its own provider.
- All consumers re-render when the value changes, so memoize the value and split contexts.
- Context is a transport, not a state manager. State still lives in useState or useReducer.
- It suits theme, locale and current user, not rapidly changing data.
Common mistakes
- Creating the value object inline in the provider so it changes on every render and re-renders every consumer.
- Putting unrelated, fast-changing data in a single large context.
- Using context as a replacement for server-state tools or for all application state.
- Forgetting that the default value is used only when there is no provider.
Follow-ups
- How would you stop a component from re-rendering when only an unrelated part of the context value changed?
- When would you choose Zustand or Redux Toolkit instead of context?
- What does useContext do differently from the older Consumer component?
Tips
- Mention the re-render behaviour early. It is the most common follow-up and the main weakness.
- Show the custom hook with an error check. It signals production habits.
- Do not say context replaces Redux. Say it covers a narrower job.