# The Context API

> How to pass data deeply through the component tree without having to pass props down manually at every single level.

Subject: React | Lesson 21 of 48 | 5 min read
Interactive lesson: https://codewithanimation.in/react/context-api
Previous: [Lifting State Up](https://codewithanimation.in/react/lifting-state-up.md) | Next: [The Context Performance Trap](https://codewithanimation.in/react/context-performance-trap.md)

## What is Context?

Context provides a way to 'teleport' data directly to components deep in the tree without passing it via props.

It consists of three parts:

**1. The Context Object:** Created via `createContext()`. This acts as the key.

**2. The Provider:** `<MyContext.Provider value={data}>`. This wraps a section of your app and broadcasts the data downwards.

**3. The Consumer:** `useContext(MyContext)`. Any component inside the Provider can use this hook to read the data, no matter how deep it is.

## The Prop Drilling Problem

If the top-level `App` knows the `currentUser`, but a deeply nested `Avatar` component 5 levels down needs it, you'd normally have to pass `currentUser` through `Header`, `Nav`, and `Profile` just to get it there.

This is called 'prop drilling' and it makes intermediate components messy and brittle.

Context solves this by letting `Avatar` directly read the `currentUser` broadcasted by `App`.

## The standard pattern

Create the context in a separate file and export it: `export const ThemeContext = createContext('light');`

In your root component, wrap your app in the provider: `<ThemeContext.Provider value='dark'><App /></ThemeContext.Provider>`

In any child component, read it: `const theme = useContext(ThemeContext);`

## Common mistake: Default Values vs Provider Values

When you call `createContext('light')`, 'light' is the *default value*.

However, a component only gets the default value if there is **no Provider** above it in the tree! If there is a Provider, but you accidentally forgot to pass a `value` prop to it, the `useContext` will return `undefined`, not the default value.

## Tip: Not a global state manager

Context is designed for data that changes *infrequently* (like themes, language settings, or the logged-in user).

If you put rapidly changing data (like the current mouse position or a ticking clock) into Context, it will force every single consumer to re-render constantly. For rapidly changing global data, use a tool like Zustand or Redux instead.

## Try it: The Teleportation Effect

Click the toggle to broadcast a new theme from the Provider. Watch how the data 'teleports' directly to the Consumer nodes, ignoring the intermediate components completely.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/context-api

## Check yourself

### Question 1

What specific problem does the Context API solve?

- A. It allows components to bypass React's virtual DOM for faster rendering.
- B. It solves 'prop drilling' by allowing deep components to read data directly from an ancestor.
- C. It allows sibling components to share state without lifting it to a parent.

Correct answer: B

Context is essentially a wormhole for props, teleporting them past intermediate components that don't need them.

- Why not A: Context has nothing to do with the virtual DOM.
- Why not C: You still need a parent Provider to share data via context.

### Question 2

Which hook do you use to read a context value?

- A. `useContext(MyContext)`
- B. `useProvider(MyContext)`
- C. `useReducer(MyContext)`

Correct answer: A

You pass the actual Context object (created by `createContext`) into the `useContext` hook to read its current value.

- Why not B: The Provider is a JSX component `<MyContext.Provider>`, not a hook.
- Why not C: `useReducer` is for complex state logic, not context.

### Question 3

When does a component receive the *default value* provided to `createContext()`?

- A. When the component calls `useContext()` but there is no matching Provider anywhere above it in the tree.
- B. When the Provider above it forgets to pass a `value` prop.
- C. When the value passed to the Provider is explicitly `null`.

Correct answer: A

The default value is only used as a fallback when the React tree cannot find a Provider for that specific context.

- Why not B: In this case, the context value is `undefined`, not the default value!
- Why not C: If the Provider's value is `null`, the consumer receives `null`.

## Remember this

- Context solves the 'prop drilling' problem.
- It requires three parts: `createContext`, the `<Provider>`, and `useContext`.
- The Provider broadcasts the data downwards.
- The Consumer reads the data, bypassing intermediate components.
- Only use Context for data that changes infrequently.
