React Reference ยท Top-Level React APIs
React Context.Provider and context as a provider
Reference for providing context in React: the React 19 shorthand <Context value>, the older Context.Provider, Context.Consumer, nesting and re-render rules.
A provider is the component that supplies a context value to everything rendered inside it. In React 19 you render the context object itself: <ThemeContext value="dark">. Before React 19 you wrote <ThemeContext.Provider value="dark">. Both forms work in React 19 and mean the same thing.
Every component below the provider that reads the context (with useContext or use) receives the value prop. When the value changes, those components re-render.
<ThemeContext value="dark"> <Page /> </ThemeContext>
Try it
Syntax
<SomeContext value={value}>{children}</SomeContext> // older, still supported <SomeContext.Provider value={value}>{children}</SomeContext.Provider> // legacy way to read a value <SomeContext.Consumer>{(value) => /* JSX */}</SomeContext.Consumer>
| Prop | Description |
|---|---|
value |
The value passed to every reader in the subtree. Any type. Compared with Object.is between renders. |
children |
The subtree that can read the value. |
A provider renders nothing of its own: it adds no DOM node and no wrapper.
Usage notes
- Pick one style per code base.
<Context value>is shorter and is what new code should use.Context.Providerwill be deprecated in a future version, and a codemod exists to convert it. Libraries that must support React 18 still need.Provider. - Missing
valuemeansundefined. A provider without avalueprop does not fall back to the default value. Readers getundefined. The default only applies when there is no provider at all. - Only readers below the provider see it. A sibling or parent of the provider gets the default (or an outer provider's value).
- Same value, no re-render. If the provider re-renders with a value that is
Object.is-equal to the previous one, consumers are skipped. An inline object or array literal is always new, so memoize it when it matters. The children passed through the provider () keep their identity and are not re-rendered by the provider's own state change unless they read the context. Context.Consumertakes a function as its only child and calls it with the current value. It works everywhere, including class components, butuseContextis simpler. In class components you can also setstatic contextType = SomeContextand readthis.context.- Several contexts are provided by nesting providers. Large apps often group them in one
AppProviderscomponent to keep the root readable. - Portals keep context. A component rendered through a portal still reads the providers of the place where it was written in the tree, not where it appears in the DOM.
Related
- createContext, useContext, memo
- Lessons: Context, Context patterns, Portals