React Context.Provider
`Context.Provider` supplies a context value to matching descendants in its component subtree. It lets components consume shared data without passing that data through every intermediate component as props.
A React context is created with `createContext`, which produces a context object that can be consumed by descendant components. A provider supplies the value associated with that context for a particular subtree.
Components below the provider can read the current value with APIs such as `useContext`. The nearest applicable provider determines which value a consumer receives, so nested providers can override the value for a smaller subtree.
When a provider's `value` changes, React can re-render components that consume that context. Because the value is compared by identity, creating a new object on every render can cause consumers to update even when the contained data is unchanged.
Context is useful for values needed by many components, such as a theme, locale, or authenticated-user information. It is not automatically a replacement for all state management, and frequently changing or unrelated state may be better kept closer to the components that use it.
Key points
- A provider makes a context value available to descendants.
- The closest matching provider supplies the value to a consumer.
- Changing the provider's `value` can cause context consumers to re-render.
- Nested providers can supply different values to different subtrees.
- Use context for shared concerns rather than avoiding every instance of prop passing.
Interview questions
Can nested `Context.Provider` components supply different values?
Yes. A nested provider can supply a different value for the same context to its subtree. Consumers inside that subtree read the inner provider's value, while consumers outside it can continue reading the outer provider's value. This allows localized overrides such as a different theme for one section of an application.
What problem does `Context.Provider` solve?
It avoids passing the same data through many intermediate components when the data is needed by a deeper descendant. A provider places a context value into a component subtree. Descendants can access it with a context consumer such as `useContext`. Common examples include themes, locale settings, and current-user information.
How does React determine which provider value a component receives?
React uses the nearest applicable provider for the requested context in the component's ancestry. If providers are nested, the inner provider supplies the value to consumers below it until another provider changes that boundary. If no matching provider exists, the consumer receives the context's default value. This makes nested providers useful for overriding a shared setting locally.
Why can an inline object passed as a provider value cause unnecessary re-renders?
A new object literal has a new identity each time the provider component renders. Context consumers can therefore observe the provider's value as changed even if the object's fields contain the same information. This can cause unnecessary consumer updates. Memoizing an appropriate value can help when the provider itself re-renders frequently.
When would you choose Context instead of passing props?
Context is useful when many components at different depths need the same value and passing it through intermediate components would add unnecessary coupling. Props are often clearer when the dependency is local and follows a direct parent-to-child relationship. Context should not be used solely to avoid writing a small number of props. The choice depends on the scope and sharing pattern of the data.