useContext
Learn how the React useContext hook shares data without prop drilling, including the React 19 syntax that lets Context act as its own provider.
On this page
1. What is useContext?
Normally, data travels from parent to child through props. But if a value (like the theme or the logged-in user) is needed by many components, passing props through every level gets tiring.
Context works like a "broadcast". Put a value in one place at the top, and any component below that needs it can pick it up with useContext.
2. Why does it matter?
This problem is called prop drilling: components in the middle receive and forward props that they do not need themselves.
| Prop drilling | With Context |
|---|---|
| App passes a theme prop to Layout | App puts the theme in Context once |
| Layout passes a theme prop to Sidebar | Sidebar does not need to pass anything |
| Sidebar passes a theme prop to Button | Button reads the theme itself with useContext |
| Add a new level and you change every place | The components in the middle stay untouched |
3. Where is it used?
| Use case | What goes in Context |
|---|---|
| Theme | "light" or "dark" mode |
| Auth | The current user and a logout function |
| Language | The selected locale, like "ur" or "en" |
| Design system | The shared size and variant of a form or table |
Context is best for values that change rarely and are needed in many places. You do not need to put everything in Context.
4. How does it work?
There are three steps: create the Context, provide a value, and read it below.
When the provider's value changes, all components that read that Context re-render. This is why you should design the value carefully.
5. Code example
Theme Context
import { createContext, useContext, useState } from "react";
type Theme = "light" | "dark";
const ThemeContext = createContext<Theme>("light");
export default function App() {
const [theme, setTheme] = useState<Theme>("light");
return (
<ThemeContext value={theme}>
<button onClick={() => setTheme(theme === "light" ? "dark" : "light")}>
Toggle theme
</button>
<Toolbar />
</ThemeContext>
);
}
function Toolbar() {
return <SaveButton />;
}
function SaveButton() {
const theme = useContext(ThemeContext);
return <button className={`btn-${theme}`}>Save</button>;
}How does the code work?
| Line | Meaning |
|---|---|
createContext<Theme>("light") | Creates the Context, with "light" as the default value |
<ThemeContext value={theme}> | All components below now have access to theme |
Toolbar | The component in the middle, which passed no props |
useContext(ThemeContext) | SaveButton read the theme directly |
A clean pattern with a custom hook
const AuthContext = createContext<{ user: string | null } | null>(null);
export function useAuth() {
const ctx = useContext(AuthContext);
if (!ctx) {
throw new Error("useAuth must be used inside AuthProvider");
}
return ctx;
}This pattern throws an error right away if the hook is used in the wrong place, and the rest of your code only needs to write useAuth().
6. Common mistakes
Mistake: Calling useContext outside a provider
Why? If no provider is found, React gives you the default value, and the bug stays hidden.
Better: Keep the component inside the provider, or add a null check in a custom hook and throw an error.
Mistake: Passing a new object as the value on every render
Why? A new object is different every time, so all consumers re-render for no reason.
Better: Keep the value in useMemo, or use React Compiler, which handles this for you.
Mistake: Putting all state into Context
Why? Fast-changing state re-renders the whole tree and makes the app feel slow.
Better: Keep local state local. Use Context only for shared values that change rarely.
Interview tip
If you are asked "Can Context replace Redux?", the answer is: Context is only a way to pass data, not state management. For large, complex state, useReducer + Context or a library like Zustand is a better fit.
7. What to learn before and after
| Stage | Topics |
|---|---|
| Before | Props, Lifting State Up, State with useState |
| Alongside | useReducer, Custom Hooks |
| After | The use() API, State Management Libraries |
Final quick revision
| Point | Detail |
|---|---|
| What it is | Reading a value from higher in the tree directly |
| Problem it solves | Prop drilling |
| How to create it | createContext(defaultValue) |
| Providing (React 19) | <MyContext value={...}> |
| Reading | useContext(MyContext) or use(MyContext) |
| Re-render | All consumers, when the value changes |