Skip to content
HooksChapter 18 / 37Intermediate4 min read

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 drillingWith Context
App passes a theme prop to LayoutApp puts the theme in Context once
Layout passes a theme prop to SidebarSidebar does not need to pass anything
Sidebar passes a theme prop to ButtonButton reads the theme itself with useContext
Add a new level and you change every placeThe components in the middle stay untouched

3. Where is it used?

Use caseWhat goes in Context
Theme"light" or "dark" mode
AuthThe current user and a logout function
LanguageThe selected locale, like "ur" or "en"
Design systemThe 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.

The Context flow
Create ThemeContext with createContext
Wrap the tree at the top with ThemeContext and a value
Call useContext(ThemeContext) in any component below
React gives it the value from the closest provider

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

tsx
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?

LineMeaning
createContext<Theme>("light")Creates the Context, with "light" as the default value
<ThemeContext value={theme}>All components below now have access to theme
ToolbarThe component in the middle, which passed no props
useContext(ThemeContext)SaveButton read the theme directly

A clean pattern with a custom hook

tsx
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

Final quick revision

PointDetail
What it isReading a value from higher in the tree directly
Problem it solvesProp drilling
How to create itcreateContext(defaultValue)
Providing (React 19)<MyContext value={...}>
ReadinguseContext(MyContext) or use(MyContext)
Re-renderAll consumers, when the value changes