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.
With Context: App provides cartItems once → any component reads it directly
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).
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.
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'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.
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
many components
across the tree?
Context
component or a
small subtree?
Props
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.
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.
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.
Quiz
useContext primarily solve?createContext() return?useContext(MyContext) return inside a component?You completed the useContext & Global State quiz.
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 →