The Provider pattern packages the data and the methods that change it into a stateful Provider component plus a matching custom hook: the state lives inside useState or useReducer, it is handed down through a Context, and the outside world only deals with useXxx() reads and writes. The Provider owns the state and decides where it applies, the hook exposes a tidy API, and the components in between never learn that a context exists at all.
Group
Component Patterns
Kind
Pattern
ℹ️ Info The editor transpiles JSX to JavaScript and renders the real UI in a sandbox; components mount automatically — no manual render call needed.
Returns:No return value; this entry describes an encoding pattern rather than a function.
▶ Example
The example runs out of the box: it auto-renders when expanded, and edits take effect instantly.
Output:
After clicking the button the cart immediately reads "Cart (1): Apple" — the state is managed in CartProvider while ItemList reads and writes it through useContext.
💡 Tip Provider plus useContext plus a custom hook is the standard trio for hand-rolled light global state; small projects do not need Redux.
ℹ️ Info Put both the state and the update function into the value so consumers can read and write in one go; place the Provider at the smallest common ancestor that needs it.
⚠️ Warning When the value is an inline object, every render of the Provider re-renders all consumers — stabilize it with useMemo.
Q Where in the component tree should the Provider sit?
A At the smallest common ancestor of all consumers; the smaller the scope, the fewer unnecessary re-renders it causes.
Q Why wrap the value in useMemo?
A An inline object is a new reference on every render, which forces every consumer to re-render; useMemo caches the reference and prevents that.
Q How does the Provider pattern differ from Redux?
A The Provider pattern is a built-in React capability that fits small and medium apps; Redux adds a uniform action pipeline, middleware and debugging tools for large applications.
Web-Tutorial Tech Team
A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.