Context: sharing values across the tree

Advanced

Introduction

Props pass data one level at a time. That is clear, but some values are needed almost everywhere: the colour theme, the signed-in user, the language of the interface. Passing them as props through every component in between, many of which do not use them, is called prop drilling, and it makes components harder to change. Context lets a component make a value available to everything below it, at any depth.

How it works

Using context takes three steps. First, create it, outside any component, with a default value:

const ThemeContext = createContext("light");

Second, provide a value by wrapping part of the tree. Since React 19 the context itself is the provider; in older code you will see <ThemeContext.Provider value={theme}>, which means the same:

<ThemeContext value={theme}>
  <Toolbar />
</ThemeContext>

Third, read it in any component below with useContext:

function Button() {
  const theme = useContext(ThemeContext);
  return <button className={"btn-" + theme}>Save</button>;
}

useContext finds the nearest provider above the component and returns its value; with no provider, it returns the default from createContext. Components in between, such as Toolbar, pass nothing and know nothing about the theme.

Context on its own is not state. To change the value, keep it in state in the component that provides it, and pass the state as the value. When the state changes, React renders every component that reads the context, wherever it is. To let deep components change the value too, provide an object with both the value and a function, such as { user, logout }, or provide a reducer's dispatch in a second context.

Context is powerful, so it is easy to overuse. Every reader renders again when the value changes, and a component that reads context is harder to reuse elsewhere, because it depends on a provider being above it. Use context for values that really are needed across the app. For data needed only one or two levels down, props are simpler and clearer, and passing JSX as children often removes drilling without any context.

Example

App.jsx
console.log output from the page shows here.

Line by line

  1. ThemeContext is created with the default "light".
  2. Button reads the theme with useContext and puts it in its class.
  3. Toolbar uses two buttons but passes no theme to them.
  4. App keeps the theme in state and provides it; switching it makes both buttons render with the new value.

Expected result

The preview shows two buttons, "Switch theme" and "Save", both with the class btn btn-light, and the note. Clicking "Switch theme" changes both to btn btn-dark.

In the real world

Apps keep the theme, the signed-in user, the language and feature flags in context. Routers provide the current location through context, and component libraries use it so that a Tabs component can share its state with every Tab inside it. Many state-management libraries are built on context combined with reducers.

Common mistakes

  • Reading a context with no provider above, and getting the default value without noticing.
  • Using context for data only one or two levels down, where props would be simpler.
  • Providing a new object on every render, which renders every reader each time.
  • Creating the context inside a component, so every render makes a new one.

Best practices

  • Create contexts at the top level of a module, with a meaningful default.
  • Provide state, or state with the functions that change it, near the top of the part of the app that needs it.
  • Wrap useContext in a small custom Hook, such as useTheme(), for a clear API.

Summary

Context makes a value available to every component below a provider, removing prop drilling. Create it with createContext, provide a value by wrapping the tree, and read it with useContext; keeping the value in state makes every reader update when it changes. Use context for app-wide values, and props for the rest.

Quick check

1. What problem does context solve?
2. What does useContext return when there is no provider above?
3. How do you make a context's value change over time?
4. When are props better than context?

Key takeaways

  • Context passes a value to every component below a provider, without props in between.
  • Read it with useContext; when the provided value changes, every reader renders again.
  • Use context for widely needed values such as the theme, the user or the language; prefer props for everything else.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.