Context API
Learning Objectives
After this lesson, you will be able to:
Define Context and distinguish a
Context.Provider
from aContext.Consumer
Use the React Context API to pass data and functions across the Component Tree
Identify appropriate use cases for React Context
What is Context in React
From the React Docs:
Context provides a way to pass data through the component tree without having to pass props down manually at every level.
In short, the Context API gives as a method to work around prop-drilling in certain situations.
We have learned that React has a uni-directonal flow of data, only passing data or functions from parent to child via props. Using Context
allows us to use data or functions that we would consider global within our Component tree without passing them through each level of a branch.
A Context
is composed of three parts: the definition, the Provider and the Consumer. Aptly named, the Provider will provide access to the data that we want to make available, and the Consumer will be used where we want to use the data provided.
You may have already been using Context whether you knew it or not. React Router is an example of the Context API in use! Behind the scenes, React Router has created a Context that includes location
, history
, match
and helper functions to deal with navigation. It grants access via the<BrowserRouter />
and <Link />
components.
Appropriate Use Cases
Authenticated User Data
Theming
Localization data
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.
Using Context
Create a Context
Touch a new file in src
to house your Context code. You can define and export data and function stubs to be used by Context. When creating your context make sure that the shape of your Context data matches what you will use be using in your Context Providers and Consumers
Context Providers
Import the Context (and any data) into the Component that you intend to provide data from.
Wrap the portion of your Component tree in which you intend to use the data with a Context Provider and pass the data to it as a value
prop.
Context Consumers
Within the child component where data should be displayed, import the Context and wrap the portion of the component with a Context.Consumer
. Your context data will be accessible through an anonymous function.
Accessing Context in Function Components
Accessing Context in Class Components
Class Components offer another method of accessing Context data by assigning a context to the Component.contextType
property. This method only works when access to a single Context is required.
Accessing Multiple Contexts
You can use data from multiple Contexts in the following manner
Note on Hooks
The new Hooks API* has provided a useContext
hook to allow access to your Contexts within function components.
Alternatives to Context
Containment
Passed
Extra Resources
Redux - Docs
Last updated