Context, and avoiding prop drilling

Intermediate · 13 min

createContext and useContext, when reaching for context actually pays off, and why every consumer re-renders when the value changes.

Before thisLifting state up and compositionuseReducer for complex state

Web Dev Simplified · 13 min

Context, and avoiding prop drilling

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Web Dev SimplifiedReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Prop drilling is passing a value through components that never use it themselves, just to hand it to a descendant several layers down.
  • createContext plus a Provider lets any descendant read a value with useContext, skipping every layer in between.
  • Context is for values that genuinely belong to a whole subtree — theme, the signed-in user, locale — not a replacement for lifting state to a shared parent when only a couple of components need it.
  • Every component that calls useContext on a given context re-renders whenever that context's value changes, even if the component only reads a field that didn't change.
  • Splitting one big context into several smaller ones, or reaching for a dedicated state library, are both ways around that re-render cost — context itself does not solve it.
  • Composition (passing JSX as children or a render prop) solves a surprising number of 'prop drilling' problems without context at all.

The problem: a value nobody in the middle actually needs

Imagine an app where the current theme name lives in the top-level App component, and a ThemeToggleButton three layers down needs to read it:

function App() {
  const [theme, setTheme] = useState("light");
  return <Page theme={theme} setTheme={setTheme} />;
}

function Page({ theme, setTheme }) {
  return <Sidebar theme={theme} setTheme={setTheme} />;
}

function Sidebar({ theme, setTheme }) {
  return <ThemeToggleButton theme={theme} setTheme={setTheme} />;
}

function ThemeToggleButton({ theme, setTheme }) {
  return (
    <button onClick={() => setTheme(theme === "light" ? "dark" : "light")}>
      Switch to {theme === "light" ? "dark" : "light"}
    </button>
  );
}

Page and Sidebar have no interest in theme. They accept it and pass it along purely because ThemeToggleButton needs it. That’s prop drilling: every component between the owner of a value and its real consumer has to carry it, and adding a fifth layer means touching a fifth signature that still doesn’t use the thing it’s passing.

Context: skip the layers in between

createContext plus useContext let a value jump straight from a Provider to any descendant that asks for it, no matter how deep:

const ThemeContext = createContext(null);

function App() {
  const [theme, setTheme] = useState("light");
  return (
    <ThemeContext.Provider value={{ theme, setTheme }}>
      <Page />
    </ThemeContext.Provider>
  );
}

function Page() {
  return <Sidebar />;
}

function Sidebar() {
  return <ThemeToggleButton />;
}

function ThemeToggleButton() {
  const { theme, setTheme } = useContext(ThemeContext);
  return (
    <button onClick={() => setTheme(theme === "light" ? "dark" : "light")}>
      Switch to {theme === "light" ? "dark" : "light"}
    </button>
  );
}

Page and Sidebar no longer mention theme at all. ThemeToggleButton reaches directly into the context created by the nearest Provider above it in the tree.

When context is actually the right call

Not every passed prop is drilling, and not every drilling problem needs context. If only two or three components are involved, lifting state to their shared parent (covered earlier in this roadmap) is usually simpler and keeps data flow explicit in the JSX. Composition helps too: a component that just renders children can carry deeply nested content through layers that never see it as a named prop at all.

Context earns its keep when a value is genuinely ambient for a whole subtree — the current theme, the signed-in user, the active locale, a feature-flag set — and when many components at unpredictable depths need to read it. Auth state is the textbook case: a CurrentUserContext set once near the root lets any page, anywhere in the tree, call useContext(CurrentUserContext) without the app’s routing or layout components needing to know or care.

The real limit: context is not a state manager

The part that trips people up: a context value change re-renders every component that calls useContext on it, regardless of which part of the value actually changed. If ThemeContext’s value were { theme, setTheme, user, cart }, updating theme would still re-render a component that only reads cart, because React compares the whole value object, not its individual fields, when deciding whether to re-render consumers.

For a value that changes rarely — theme, locale — this is harmless. For something that changes constantly across a large subtree, it becomes real waste. The usual fixes are splitting one busy context into several narrower ones, memoizing the value object so it doesn’t get a new identity on every render of the provider, or moving to a store library built for selective subscriptions (covered later in this roadmap’s data-and-state group). Context is a plumbing mechanism for passing a value down a tree; it has no idea, by itself, which consumers actually care about which part of that value.

What the video covers

Web Dev Simplified’s Learn useContext In 13 Minutes opens from exactly this prop-drilling problem and builds to a working provider/consumer example, which is the mechanical core of this lesson. It moves quickly past the re-render cost of context and past the judgment call of when a smaller fix (composition, lifting state) beats reaching for context at all — both covered above. Jack Herrington’s deeper link picks up precisely that thread, walking through when context stops being enough and a real state manager earns its place.

Where this fits

Once a value is shared through context, a natural next step is sharing it and its behavior through a component’s own public API. Next: compound components, where context becomes the hidden wiring behind components that look like plain JSX to whoever uses them.

Check yourself

  1. 01What makes passing a prop through a component 'prop drilling' rather than just normal data flow?

    It is prop drilling specifically when the intermediate component has no use for the value itself and only exists to forward it to something further down the tree. A component that reads and uses the prop it receives is not drilling anything.

  2. 02If a context's value is an object `{ user, theme }` and only `theme` changes, which components re-render?

    Every component that calls useContext on that context re-renders, including ones that only read `user`. React does not diff the fields of the value for you; it treats the whole value as one unit.

  3. 03Why is context a poor fit for state that changes on every keystroke, like a search input's text?

    Because every consumer of that context re-renders on every change, and with a fast-changing value that is a lot of re-rendering across a potentially large subtree. Local state, or a store library built to let components subscribe to slices of state, fits that case better.

  4. 04Besides adding context, what is another way to avoid drilling a prop through components that don't use it?

    Composition. Passing the deeply-nested piece as `children` (or another JSX prop) to an outer component lets that component render whatever was handed to it, without the layers in between needing to know what that content is or forward any extra props.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark