liveBook · Manning

Provider pattern

As explained in React in Depth

The Provider pattern is a component-structuring pattern in which a provider supplies shared values to descendant components, commonly through React context.

The Provider pattern puts related state and actions behind a React context and makes them available to descendants at any depth. A context can supply more than one value: a dark-mode toggle, for example, needs both the current mode and a function that changes it. section 2.1 section 2.1.1

Move state into a dedicated provider

Create a context, then let a provider own the state and pass an object containing isDarkMode and toggleDarkMode as its value. App can render <DarkModeProvider><Main /></DarkModeProvider>, while consumers read the object through a useDarkMode custom hook. This removes context wiring from App. Because App no longer owns the toggle state, its own re-renders no longer drive Main re-renders; components that consume the changing context still update. section 2.1.2 section 2.1.1

Scale the pattern deliberately:

  • Use separate contexts at different levels for distinct concerns, such as authorization, application data, and UI controls; one provider need not own everything. section 2.1.5
  • For larger data features, keep storage and manipulation in the provider and expose focused hooks for reads and actions. A data-provider example persists state to localStorage and separates its context, provider, and access hooks into files. section 8.3.2
  • Watch update costs. Memoization can spare a non-consuming component from a parent re-render, while selectability targets unnecessary updates when consumers need only part of a context value. section 2.1.1 section 2.1
  • When testing a consumer alone, wrap it in a provider supplying the values or callbacks it expects; otherwise it sees the context default. In React Developer Tools, inspect the nearest provider to manipulate its value rather than trying to edit it from a consumer. section 10.3.3 section 4.4.3

This is a flexible way to organize shared data, not a blanket reason to put all state in context. The examples use <MyContext.Provider> and useContext; React 19 also permits <MyContext value={...}> and use(). Comparisons with other state-management tools remain a separate design decision. section 2.1.1 section 2.1.5

How React in Depth covers it

How deep it goes
Builds from a multi-value context to a dedicated provider and introduces selective consumption; defers broader state-management comparisons.
Examples
Uses a dark-mode toggle, a persistent data provider with focused access hooks, and isolated tests with mock context values.
What it assumes
Assumes existing React context knowledge and develops multi-value provider structure from that starting point.
What you can do after
Build it from scratch with a dedicated React provider and consumer hooks, then test consumers under supplied context values.
How it connects
Connects React context, custom hooks, memoization, selective context consumption, component testing, and React Developer Tools.

Also explained in

Compare how 2 books explain provider pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React in Depth. The section links point to the parts of the book it draws on.