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

The Context Object Structure

Exploring the Provider and Consumer properties returned by createContext and how they form a pair.

The Context Object Structure

You'll learn what createContext actually returns and how Provider and Consumer work as a matching pair.

What createContext Gives You

When you call createContext(), React doesn't just give you a single object — it returns a context object with two key properties: Provider and Consumer. Think of these as two ends of a communication channel: one broadcasts data (Provider), and one listens for it (Consumer).

const MyContext = createContext(defaultValue);

// MyContext now has:
// - MyContext.Provider
// - MyContext.Consumer

The Two Sides of the Channel

Provider is a component that wraps part of your tree and supplies a value. Any component nested inside can access that value:

<MyContext.Provider value={/* some data */}>
  {/* child components */}
</MyContext.Provider>

Consumer is how components originally accessed that value (before hooks). It uses a render prop pattern:

<MyContext.Consumer>
  {value => /* render something using value */}
</MyContext.Consumer>

These two properties are permanently linked — a Consumer only reads from its matching Provider. If you create ThemeContext, only ThemeContext.Consumer can read values from ThemeContext.Provider.

Why This Structure Matters

This pairing ensures type safety and prevents mix-ups. You can't accidentally read from the wrong context. The Provider and Consumer are literally parts of the same object, guaranteeing they communicate correctly.

In modern React, you'll mostly use useContext(MyContext) instead of the Consumer component, but the structure remains the same.

Key Takeaway: createContext() returns an object with Provider and Consumer properties that form an inseparable pair — one broadcasts data, the other receives it, and they're permanently matched to each other.