Chapter 11 · Managing State

Context

Pass data to any component far below, without threading props through every level in between.

You will learn
  • What "prop drilling" is
  • The three steps of context: create, provide, use
  • When to use context, and when not to
  • How to combine context with a reducer for app-wide state

The problem: prop drilling

Props are great, but when a value is needed deep in the tree, you have to pass it through every component in between, even those that don't use it:

This is called prop drilling. Context lets a parent make data available to everything below it, however deep.

Context in three steps

Step 1: create the context

The default value is used when there's no provider above the component.

Step 2: use it in a component

Step 3: provide it from a parent

Older syntax Before React 19 you wrote <ThemeContext.Provider value={theme}>. You'll still see it in tutorials, and it still works. React 19 lets you use <ThemeContext value={...}> directly.

Now any component inside <ThemeContext value="dark">, at any depth, gets "dark" from useContext(ThemeContext). When theme state changes, every component that reads it re-renders automatically.

The closest provider wins

Context works like CSS inheritance: a value flows down until something closer overrides it.

Before you use context

Context is tempting, but it makes data flow harder to follow. Try these first:

  1. Just pass props. Ten props through three levels is fine, because it's explicit and clear.
  2. Use children. If a middle component only passes data along, let it take JSX as children instead:

Good uses for context: theme (dark/light), the current logged-in user, language, routing, and app-wide state managed with a reducer (next section).

Scaling up: reducer + context

Combine Chapter 10's reducer with context, and any component in the tree can read the tasks and dispatch changes, without passing props.

Step 1: create two contexts

Step 2: a provider component that holds the reducer

Step 4: use it anywhere

Now TaskApp is clean, all the logic lives in one file, and any component can reach the tasks.

More examples

Example 1: automatic heading levels

A Section reads the level from above and provides level + 1 to everything inside it:

Example 2: providing an object with data and functions

Example 3: a safe custom hook

If you forget to wrap the app in the provider, you get a clear error message instead of a confusing "cannot read properties of null".

Example 4: translations

Recap

  • Context solves prop drilling: createContext → <Ctx value={...}> → useContext(Ctx).
  • A component reads the value from the closest provider above it.
  • Try plain props or children before reaching for context.
  • Reducer + context (wrapped in a provider component and custom hooks) is a simple way to manage app-wide state.

Exercises

1. Theme toggle

Create a ThemeContext. In App, keep theme in state with a toggle button. Make a deeply nested Card component that switches between a white and a black background based on the theme, without receiving any props.

Show solution

2. Current user

Create a UserContext holding { name: 'Shashank' }, and show "Welcome, Shashank" in a Header component three levels deep.

Show answer

The same pattern: const UserContext = createContext(null), wrap the app in <UserContext value={user}>, and read it with const user = useContext(UserContext) in Header.

3. No provider? 🟢 easy

With const ThemeContext = createContext('light'), what does useContext(ThemeContext) return in a component that has no <ThemeContext> above it?

Show answer

'light', the default value passed to createContext.

4. Predict the nesting 🟢 easy

Show answer

A: large · B: small · C: small (the depth doesn't matter, only the closest provider) · D: large.

5. Language switcher 🟢 easy

Using Example 4, keep lang in state in App, and add buttons "English" and "हिन्दी" that switch every <T> on the page.

Show solution

6. Remove the prop drilling 🟡 medium

Show solution

7. Auth flow 🟡 medium

Using Example 2's AuthProvider, build a Header showing "Hi, Asha" or "Not logged in", and a separate LoginPage with a name input and a Login button. They should stay in sync without passing any props.

Show solution

8. A useTheme hook with a safety check 🟡 medium

Create a ThemeProvider that holds theme and toggleTheme, and a useTheme() hook that throws a helpful error when it's used outside the provider.

Show solution

9. Auto heading levels 🟡 medium

Type Example 1 into your project. Then add a Section four levels deep and check in DevTools → Elements that it renders an <h5>. What happens at level 8?

Show answer

Thanks to Math.min(level, 6), anything deeper than 6 stays <h6>, because HTML has no <h7>.

10. A cart with reducer + context 🔴 hard

Build a CartProvider using your cartReducer from Chapter 10, with useCart() and useCartDispatch() hooks. A Header shows "🛒 3", a ProductList has "Add to cart" buttons, and the two components share no props.

Show solution