Course contentsShow
← React
Lesson 777 of 2,39620. useReducerFree lesson

The Global State Problem That useReducer + Context Solves

When local state grows complex and needs sharing across distant components without prop drilling.

The Global State Problem That useReducer + Context Solves

What you'll learn: When and why combining useReducer with Context eliminates prop drilling for complex shared state.

The Prop Drilling Nightmare

Imagine your app has a shopping cart. The cart's state lives in <App>, but you need to add items from deeply nested product components, show the count in a header, and display the total in a sidebar. You'd have to pass cart, addToCart, removeFromCart, updateQuantity through every component in between—even ones that don't care about the cart at all.

<App cart={cart} addToCart={addToCart} ...>
  <Layout cart={cart} addToCart={addToCart} ...>
    <Main cart={cart} addToCart={addToCart} ...>
      <ProductList addToCart={addToCart} ...>
        <ProductCard addToCart={addToCart} />

This is prop drilling—threading props through layers that only act as middlemen.

The Solution: Reducer + Context

When state is complex (like our cart with multiple operations) and needs to be accessible across distant components, useReducer + Context is the sweet spot:

  • useReducer handles the complex state logic centrally (all cart operations in one reducer)
  • Context teleports both state and dispatch to any component that needs them—no drilling

Now <ProductCard> can useContext(CartContext) to get dispatch, and call dispatch({ type: 'ADD_ITEM', payload: product }) directly. The header reads state.items.length. No intermediate components touched.

Key Takeaway

useReducer + Context solves the problem of complex state that needs to be shared across many distant components—combining centralized logic with drilling-free access.