Course contentsShow
← React
Lesson 798 of 2,39621. Context FundamentalsFree lesson

The Legacy Context.Consumer Pattern

Understanding the render-props-based Consumer component and why useContext replaced it in modern code.

The Legacy Context.Consumer Pattern

You'll learn how React's original render-props-based Consumer worked and why modern code uses useContext instead.

The Old Way: Context.Consumer

Before React Hooks existed, consuming context required using a special Context.Consumer component that used the render props pattern. Instead of calling a hook, you'd wrap your JSX with <Context.Consumer> and provide a function as its child:

<MyContext.Consumer>
  {value => (
    <div>
      <p>The context value is: {value}</p>
    </div>
  )}
</MyContext.Consumer>

The function receives the context value as an argument and returns the JSX to render.

Why It Was Clunky

Multiple contexts = nesting nightmare:

<ThemeContext.Consumer>
  {theme => (
    <UserContext.Consumer>
      {user => (
        <SettingsContext.Consumer>
          {settings => (
            <div style={{background: theme.bg}}>
              Hello {user.name}, settings: {settings.lang}
            </div>
          )}
        </SettingsContext.Consumer>
      )}
    </UserContext.Consumer>
  )}
</ThemeContext.Consumer>

This "pyramid of doom" made code hard to read and maintain.

The Modern Replacement

The useContext hook replaced this pattern entirely:

const theme = useContext(ThemeContext);
const user = useContext(UserContext);
const settings = useContext(SettingsContext);

return <div style={{background: theme.bg}}>
  Hello {user.name}, settings: {settings.lang}
</div>;

Clean, flat, readable.

When You Might See It

You'll encounter Context.Consumer in older codebases or libraries written before hooks (pre-React 16.8). It still works, but there's no reason to use it in new code.

Key Takeaway: Context.Consumer was React's original render-props approach to consuming context; useContext replaced it with a simpler, more readable hook-based API that avoids deep nesting.