Getting Started with React Context API

Getting Started with React Context API

Quick Walkthrough

Table of contents

No heading

No headings in the article.

React Context API is a way to share data between components in a React application without having to pass props down through multiple levels of the component tree. This can be particularly useful for sharing data that is required by many components, such as the currently authenticated user or the application's theme. In this article, we will go over the basics of the React Context API and how to use it in a React application.

Before diving into the React Context API, it's important to understand the concept of prop drilling. Prop drilling is the process of passing props down through multiple levels of the component tree in order to make them available to a particular component. While this is a common practice in React, it can become cumbersome when the props need to be passed through multiple levels of the component tree. This is where the React Context API comes in.

The React Context API allows you to create a provider component that wraps the components that need access to the shared data. The provider component then stores the shared data in a context object, which can be accessed by any component wrapped by the provider. This means that the shared data is available to all components within the provider, without the need for prop drilling.

To get started with the React Context API, you first need to create a context object. This is done using the React.createContext() function, which returns an object with a provider and a consumer. The provider is used to store the shared data, and the consumer is used to access the data from the provider.

Here's an example of how to create a context object for storing the currently authenticated user in a React application:

const AuthContext = React.createContext();

Next, you need to wrap the components that need access to the shared data in a provider component. The provider component is created using the AuthContext.Provider component, which takes a value prop that contains the shared data.

Here's an example of how to wrap the root component of a React application in an AuthContext.Provider component:

class App extends React.Component {
  render() {
    return (
      <AuthContext.Provider value={this.state.currentUser}>
        <div>
          {/* The rest of the application goes here */}
        </div>
      </AuthContext.Provider>
    );
  }
}

Now that the provider has been set up, any component within the provider can access the shared data by using the AuthContext.Consumer component. The AuthContext.Consumer component takes a function as a child, which is called with the value from the provider.

Here's an example of how to use the AuthContext.Consumer component to access the currently authenticated user in a React component:

class Header extends React.Component {
  render() {
    return (
      <AuthContext.Consumer>
        {currentUser => {
          return <div>Welcome, {currentUser.name}</div>;
        }}
      </AuthContext.Consumer>
    );
  }
}

In addition to the AuthContext.Consumer component, you can also use the useContext hook to access the shared data from a functional component. The useContext hook takes a context object as an argument and returns the value from the provider.

Here's an example of how to use the useContext hook to access the currently authenticated user in a functional component:

import { useContext } from 'react';

function Header