Explain new Context API in React (original) (raw)

Last Updated : 16 Oct, 2024

Context API in React is used to share data across the components without passing the props manually through every level. It allows to create global state of data providing global access to all the components.

In this article, we will discuss about the context API in React and its uses with implementation.

What is Context API?

Context API is used to pass global variables anywhere in the code without the prop drilling. It helps when there is a need for sharing state between a lot of nested components. It is light in weight and easier to use, to create a context just need to call React.createContext(). No need to install other dependencies or third-party libraries like redux for state management.

Why is Context API used?

Context API solves the problem of prop drilling in React. Prop Drilling occurs when data is to be passed between multiple layers before finally sending it to the required component. This makes the application slower. This problem is solved by Context API as it creates global variables to be used throughout the application without any middle components involved. It is also easier to use than React Redux

**Working

To work with Context API we need React.createContext. It has two properties Provider and Consumer. The Provider acts as a parent it passes the state to its children whereas the Consumer uses the state that has been passed.

Benefits of Context API over React Redux

Steps to Implement Context API in React

**Step 1: Create a React application using the following command.

npx create-react-app project

**Step 2: After creating your project folder(i.e. project), move to it by using the following command.

cd project

**Step 3: We will create two new files one is _Context.js to create our context and another file named as _WelcomePage.js to create a component named as _welcomePage.

**Project Structure:

It will look like this.

Project Structure

**Apporach

We will use Context API to display a user's name and ID.

**Example: Write the following code in respective files

// Context.js import React from 'react';

const UserContext =React.createContext();

export const Provider = UserContext.Provider; export const Consumer = UserContext.Consumer;

JavaScript

// WelcomePage.js

import React from "react"; import { Consumer } from "./Context";

const WelcomePage = () => { return (

Welcome User :

{(value) => (

Name: {value.name} id :{value.id}{" "}

)} //this function takes the value as prop
); };

export default WelcomePage;

JavaScript

// index.js

import React from 'react'; import ReactDOM from 'react-dom'; import App from './App' import {Provider} from './Context'

ReactDOM.render( <Provider value={{name:"Geeksforgeeks", id:195}}> , document.getElementById('root') );

JavaScript

// App.js

import WelcomePage from "./WelcomePage";

function App() { return (

); }

export default App;

`

**Step to Run Application: Run the application using the following command from the root directory of the project.

npm start

**Output: