1. The Prop Drilling Problem

When you first learn React, passing data via props feels natural. A parent component holds the state, and it passes what each child needs. Clean, explicit, predictable.

But real applications have deep component trees. Imagine UrbanCart's layout: App holds the cart items. The cart count needs to appear in the Header. The cart total needs to appear in CartSummary. Both are nested several levels down. To get cart data there, you'd have to pass it through every intermediate component — even ones that don't use it at all.

This is prop drilling: threading data through layers of components just so a deeply nested component can access it.

Prop Drilling — Data passes through components that don't need it
App (holds cartItems)
cartItems ↓
Layout (doesn't need it)
cartItems ↓
PageContainer (doesn't need it)
cartItems ↓
Header ✓ uses it
CartSummary ✓ uses it

With Context: App provides cartItems once → any component reads it directly

The rule of thumb: If you're passing a prop through 2+ components that don't use it — just to get it to one that does — it's time for Context.

2. createContext

createContext() is where everything starts. You call it once — usually in its own file — and it returns a context object. That object has two important pieces: a Provider and a Consumer (though with hooks, you'll use the Consumer rarely).

CartContext.js Creating a Context
🏗️

What createContext() returns

A context object. It's not a component. It's not data. It's the channel through which data will flow. Think of it as declaring a global radio frequency — you haven't broadcast anything yet.

📁

Where to put it

Create a dedicated file per context: CartContext.js, ThemeContext.js, AuthContext.js. Import it wherever you need to provide or consume the context.

3. Context Provider

The Provider is a component that wraps part of your tree and broadcasts a value to every component inside it. Any component inside the Provider (at any depth) can read that value.

The critical rule: the Provider must be placed above all components that need the context. Usually this means wrapping it near the top of your app.

App.jsx Wrapping the tree with a Provider
Important: When the value prop on a Provider changes, every component that consumes that context re-renders. This is intentional — it's how the data flows. For performance-sensitive apps, you can optimise by memoising the value object, but don't worry about that yet.
🛒
UrbanCart Example
Cart state lives in one place, used everywhere

UrbanCart's App component wraps everything in a CartContext.Provider. It holds the cart state and the addToCart / removeFromCart functions. The Header reads the cart count. ProductList calls addToCart when the user clicks a button. CartSummary lists the items and calls removeFromCart. None of them receive props — they all read directly from context.

4. useContext Hook

The useContext hook is how a component reads a context value. You pass it the context object (the one returned by createContext()) and it returns whatever value the nearest Provider above is broadcasting.

Header.jsx Consuming context with useContext
ProductList.jsx Calling context functions
How useContext finds the value
Component tree: CartContext.Provider value={cartItems, addToCart, removeFromCart} └── Header useContext(CartContext) → { cartItems, addToCart, removeFromCart } ✓ └── ProductList useContext(CartContext) → { cartItems, addToCart, removeFromCart } ✓ └── CartSummary useContext(CartContext) → { cartItems, addToCart, removeFromCart } ✓ React walks UP the tree to find the nearest Provider above the component. No Provider above? Returns the default value from createContext().
Custom hook pattern: It's common to wrap useContext in a custom hook for cleaner imports. Create a useCart() hook that does return useContext(CartContext). Then components just call const { cartItems } = useCart() — no need to import the context object itself.

5. Context vs Props — When to Use Each

Context is powerful, but it's not a replacement for props. Using context everywhere creates components that are harder to test, reuse, and reason about. Here's the decision framework:

⚡

Use Context for

  • Cart state (used by Header, ProductList, CartSummary)
  • Current user / authentication status
  • Theme (light/dark) — affects every component
  • Language / locale settings
  • App-wide notifications or toasts
🔩

Use Props for

  • Data a component receives from its direct parent
  • Local UI state (is this accordion open?)
  • Configuration of a reusable component (Button color, size)
  • Event handlers passed to a child
  • Anything used by only 1–2 nearby components
Decision: Context or Props?
Is the data used by
many components
across the tree?
Yes →
Use
Context
Is it local to one
component or a
small subtree?
Yes →
Use
Props
Don't over-use context: A common mistake is moving all state into context "to be safe." This makes components harder to reuse in isolation and causes unnecessary re-renders. Keep local UI state local — only lift to context when multiple distant components genuinely need the same data.

6. CartContext — The Complete Pattern

Here is the full, production-ready CartContext pattern for UrbanCart. Notice the structure: context creation, a CartProvider component that encapsulates all cart logic, and a custom useCart hook for clean consumption.

context/CartContext.jsx Complete CartContext with Provider + custom hook
main.jsx Wrapping the app with CartProvider
Header.jsx Consuming CartContext via useCart()

7. Try It Yourself — CartContext

This editor has a full working CartContext. UrbanCart's Header shows the cart count, ProductList lets you add items, and CartSummary shows the full cart — all connected via context, zero prop drilling. Hit Run, then add some products.

Try It Yourself — UrbanCart CartContext
Preview

8. Try It Yourself — ThemeContext

Context isn't just for cart state. Any value that multiple components need can live in context. This editor shows a ThemeContext for light/dark mode. Toggle the theme button — every component responds simultaneously, with no props passed down.

Try It Yourself — ThemeContext Light/Dark Toggle
Preview

Quiz

Question 1 of 5
What problem does useContext primarily solve?
Question 2 of 5
What does createContext() return?
Question 3 of 5
Where must the Context Provider be placed in the component tree?
Question 4 of 5
What does useContext(MyContext) return inside a component?
Question 5 of 5
When should you NOT use Context for state?
0
/ 5
Great job!

You completed the useContext & Global State quiz.

Next Lesson

useReducer & Complex State →
Next: manage complex state transitions with useReducer — the step before Redux.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

You can now share state across your entire React app.
Now build the full-stack career to match.

Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses →