What are Provider and Consumer in the Context API ?
Last Updated :
28 Apr, 2025
Context API is a term that is created to pass as a global variable and can be accessed anywhere in the code. It is another way to pass props from child to parent i.e. known as "prop drilling". It has two main components "The Context Provider " and "The Context Consumer".
The Context Provider:
The Provider is a component provided by React that allows its child components to subscribe to a certain context. It accepts a value prop which is the data that will be shared with all components that are consumers of that context.
Typically, you wrap the top-level component of your application with a Provider component, thus making the context available to all its descendants.
JavaScript
// Context creation
const MyContext = React.createContext();
// Top-level Provider
const App = () => {
return (
<MyContext.Provider value={"Hello"}>
<ChildComponent />
</MyContext.Provider>
);
};
The Context Consumer:
The Consumer is a component that allows components to subscribe to a context. It lets you access the context value and use it within your component's render function.
Consumers can be nested within each other to access multiple contexts if needed.
JavaScript
// Consuming context
const ChildComponent = () => {
return (
<MyContext.Consumer>
{value => <div>{value}</div>}
</MyContext.Consumer>
);
};
How to create context API ?
Step 1: First, you need to create a context object using the createContext function from the 'react' library. This context object will hold the data that you want to share across your application.
JavaScript
import { createContext } from 'react';
export const MyContext = createContext("");
Step 2: Once you've created a context object, you need to wrap the components that need access to the shared data with a Provider component. The Provider component accepts a "value" prop that holds the shared data, and any component that is a child of the Provider component can access that shared data.
JavaScript
// Create a parent component that wraps child components with a Provider
import { useState, React } from "react";
import { MyContext } from "./MyContext";
import MyComponent from "./MyComponent";
function App() {
const [text, setText] = useState("");
return (
<div>
<MyContext.Provider value={{ text, setText }}>
<MyComponent />
</MyContext.Provider>
</div>
);
}
export default App;
Step 3: Now that we've created the provider component, we need to consume the context in other components. To do this, we use the "useContext" hook from React.
JavaScript
import { useContext } from 'react';
import { MyContext } from './MyContext';
function MyComponent() {
const { text, setText } = useContext(MyContext);
return (
<div>
<h1>{text}</h1>
<button onClick={() =>
setText('Hello, world!')}>
Click me
</button>
</div>
);
}
export default MyComponent;
Conclusion:
Providers and Consumers are fundamental elements of the Context API in React. Providers wrap the portion of the component tree where the context is available, passing down the data through the value
prop. Consumers, on the other hand, subscribe to this context, accessing the provided data and reacting to changes within it. Together, Providers and Consumers facilitate the management of global state and make it easier to share data between components in a React application, ultimately improving code maintainability and reducing prop drilling.
Similar Reads
What is the React Context API? In the React ecosystem, as your application grows, passing data down through component hierarchies can become cumbersome. This is where the Context API steps in, providing a centralized way to manage state across components.Table of ContentWhat is the Context API?How Context API Works:Steps to Creat
3 min read
How to set up the Provider Component in a React application ? In React applications, the Provider component is often used in conjunction with context to provide data to components deep down in the component tree without having to explicitly pass props through every level of the tree. Prerequisites:JavaScriptReactNode & NPMSteps to Create React Application:
3 min read
What is the difference between declarations, providers, and import in NgModule? Let us first discuss about these terms: Declarations: Declarations are used to declare components, directives, pipes that belongs to the current module. Everything inside declarations knows each other.Declarations are used to make directives (including components and pipes) from the current module a
2 min read
Comparing Redux and Context API in React: A Comprehensive Analysis In this article, we are going to learn about the difference between the Redux and Context APIs. We will cover each of them entirely with their live examples, and then we will cover the differences between them. Table of Content Context APIReduxDifference between Redux and Context APIConclusionContex
5 min read
What is the API Gateway Pattern ? API - It stands for "Application Programming Interface". In general APIs are built-in libraries or collections of libraries that perform some specific task or function. In general, we use API to get connected with other web applications. We generally connect with other web applications by following
3 min read