--- id: context title: Context permalink: docs/context.html --- Context provides a way to pass data through the component tree without having to pass props down manually at every level. In a typical React application, data is passed top-down (parent to child) via props, but this can be cumbersome for certain types of props (e.g. locale preference, UI theme) that are required by many components within an application. Context provides a way to share values like these between components without having to explicitly pass a prop through every level of the tree. - [When to Use Context](#when-to-use-context) - [Before You Use Context](#before-you-use-context) - [API](#api) - [React.createContext](#reactcreatecontext) - [Context.Provider](#contextprovider) - [Class.contextType](#classcontexttype) - [Context.Consumer](#contextconsumer) - [Examples](#examples) - [Dynamic Context](#dynamic-context) - [Updating Context from a Nested Component](#updating-context-from-a-nested-component) - [Consuming Multiple Contexts](#consuming-multiple-contexts) - [Caveats](#caveats) - [Legacy API](#legacy-api) ## When to Use Context {#when-to-use-context} Context is designed to share data that can be considered "global" for a tree of React components, such as the current authenticated user, theme, or preferred language. For example, in the code below we manually thread through a "theme" prop in order to style the Button component: `embed:context/motivation-problem.js` Using context, we can avoid passing props through intermediate elements: `embed:context/motivation-solution.js` ## Before You Use Context {#before-you-use-context} Context is primarily used when some data needs to be accessible by *many* components at different nesting levels. Apply it sparingly because it makes component reuse more difficult. **If you only want to avoid passing some props through many levels, [component composition](/docs/composition-vs-inheritance.html) is often a simpler solution than context.** For example, consider a `Page` component that passes a `user` and `avatarSize` prop several levels down so that deeply nested `Link` and `Avatar` components can read it: ```js // ... which renders ... // ... which renders ... // ... which renders ... ``` It might feel redundant to pass down the `user` and `avatarSize` props through many levels if in the end only the `Avatar` component really needs it. It's also annoying that whenever the `Avatar` component needs more props from the top, you have to add them at all the intermediate levels too. One way to solve this issue **without context** is to [pass down the `Avatar` component itself](/docs/composition-vs-inheritance.html#containment) so that the intermediate components don't need to know about the `user` or `avatarSize` props: ```js function Page(props) { const user = props.user; const userLink = ( ); return ; } // Now, we have: // ... which renders ... // ... which renders ... // ... which renders ... {props.userLink} ``` With this change, only the top-most Page component needs to know about the `Link` and `Avatar` components' use of `user` and `avatarSize`. This *inversion of control* can make your code cleaner in many cases by reducing the amount of props you need to pass through your application and giving more control to the root components. However, this isn't the right choice in every case: moving more complexity higher in the tree makes those higher-level components more complicated and forces the lower-level components to be more flexible than you may want. You're not limited to a single child for a component. You may pass multiple children, or even have multiple separate "slots" for children, [as documented here](/docs/composition-vs-inheritance.html#containment): ```js function Page(props) { const user = props.user; const content = ; const topBar = ( ); return ( ); } ``` This pattern is sufficient for many cases when you need to decouple a child from its immediate parents. You can take it even further with [render props](/docs/render-props.html) if the child needs to communicate with the parent before rendering. However, sometimes the same data needs to be accessible by many components in the tree, and at different nesting levels. Context lets you "broadcast" such data, and changes to it, to all components below. Common examples where using context might be simpler than the alternatives include managing the current locale, theme, or a data cache. ## API {#api} ### `React.createContext` {#reactcreatecontext} ```js const MyContext = React.createContext(defaultValue); ``` Creates a Context object. When React renders a component that subscribes to this Context object it will read the current context value from the closest matching `Provider` above it in the tree. The `defaultValue` argument is **only** used when a component does not have a matching Provider above it in the tree. This can be helpful for testing components in isolation without wrapping them. Note: passing `undefined` as a Provider value does not cause consuming components to use `defaultValue`. ### `Context.Provider` {#contextprovider} ```js ``` Every Context object comes with a Provider React component that allows consuming components to subscribe to context changes. Accepts a `value` prop to be passed to consuming components that are descendants of this Provider. One Provider can be connected to many consumers. Providers can be nested to override values deeper within the tree. All consumers that are descendants of a Provider will re-render whenever the Provider's `value` prop changes. The propagation from Provider to its descendant consumers is not subject to the `shouldComponentUpdate` method, so the consumer is updated even when an ancestor component bails out of the update. Changes are determined by comparing the new and old values using the same algorithm as [`Object.is`](//developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is#Description). > Note > > The way changes are determined can cause some issues when passing objects as `value`: see [Caveats](#caveats). ### `Class.contextType` {#classcontexttype} ```js class MyClass extends React.Component { componentDidMount() { let value = this.context; /* perform a side-effect at mount using the value of MyContext */ } componentDidUpdate() { let value = this.context; /* ... */ } componentWillUnmount() { let value = this.context; /* ... */ } render() { let value = this.context; /* render something based on the value of MyContext */ } } MyClass.contextType = MyContext; ``` The `contextType` property on a class can be assigned a Context object created by [`React.createContext()`](#reactcreatecontext). This lets you consume the nearest current value of that Context type using `this.context`. You can reference this in any of the lifecycle methods including the render function. > Note: > > You can only subscribe to a single context using this API. If you need to read more than one see [Consuming Multiple Contexts](#consuming-multiple-contexts). > > If you are using the experimental [public class fields syntax](https://babeljs.io/docs/plugins/transform-class-properties/), you can use a **static** class field to initialize your `contextType`. ```js class MyClass extends React.Component { static contextType = MyContext; render() { let value = this.context; /* render something based on the value */ } } ``` ### `Context.Consumer` {#contextconsumer} ```js {value => /* render something based on the context value */} ``` A React component that subscribes to context changes. This lets you subscribe to a context within a [function component](/docs/components-and-props.html#function-and-class-components). Requires a [function as a child](/docs/render-props.html#using-props-other-than-render). The function receives the current context value and returns a React node. The `value` argument passed to the function will be equal to the `value` prop of the closest Provider for this context above in the tree. If there is no Provider for this context above, the `value` argument will be equal to the `defaultValue` that was passed to `createContext()`. > Note > > For more information about the 'function as a child' pattern, see [render props](/docs/render-props.html). ## Examples {#examples} ### Dynamic Context {#dynamic-context} A more complex example with dynamic values for the theme: **theme-context.js** `embed:context/theme-detailed-theme-context.js` **themed-button.js** `embed:context/theme-detailed-themed-button.js` **app.js** `embed:context/theme-detailed-app.js` ### Updating Context from a Nested Component {#updating-context-from-a-nested-component} It is often necessary to update the context from a component that is nested somewhere deeply in the component tree. In this case you can pass a function down through the context to allow consumers to update the context: **theme-context.js** `embed:context/updating-nested-context-context.js` **theme-toggler-button.js** `embed:context/updating-nested-context-theme-toggler-button.js` **app.js** `embed:context/updating-nested-context-app.js` ### Consuming Multiple Contexts {#consuming-multiple-contexts} To keep context re-rendering fast, React needs to make each context consumer a separate node in the tree. `embed:context/multiple-contexts.js` If two or more context values are often used together, you might want to consider creating your own render prop component that provides both. ## Caveats {#caveats} Because context uses reference identity to determine when to re-render, there are some gotchas that could trigger unintentional renders in consumers when a provider's parent re-renders. For example, the code below will re-render all consumers every time the Provider re-renders because a new object is always created for `value`: `embed:context/reference-caveats-problem.js` To get around this, lift the value into the parent's state: `embed:context/reference-caveats-solution.js` ## Legacy API {#legacy-api} > Note > > React previously shipped with an experimental context API. The old API will be supported in all 16.x releases, but applications using it should migrate to the new version. The legacy API will be removed in a future major React version. Read the [legacy context docs here](/docs/legacy-context.html).