Skip to content
Tutorials Center
My learning

Topics

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.

JSX
<ThemeContext value="dark">
  <Page />
</ThemeContext>

Try it

Both provider forms
Updating the value re-renders consumers

Syntax

JSX
<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.Provider will be deprecated in a future version, and a codemod exists to convert it. Libraries that must support React 18 still need .Provider.
  • Missing value means undefined. A provider without a value prop does not fall back to the default value. Readers get undefined. 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.Consumer takes a function as its only child and calls it with the current value. It works everywhere, including class components, but useContext is simpler. In class components you can also set static contextType = SomeContext and read this.context.
  • Several contexts are provided by nesting providers. Large apps often group them in one AppProviders component 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.