Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • React Tutorial
  • React Exercise
  • React Basic Concepts
  • React Components
  • React Props
  • React Hooks
  • React Router
  • React Advanced
  • React Examples
  • React Interview Questions
  • React Projects
  • Next.js Tutorial
  • React Bootstrap
  • React Material UI
  • React Ant Design
  • React Desktop
  • React Rebass
  • React Blueprint
  • JavaScript
  • Web Technology
Open In App
Next Article:
Explain new Context API in React
Next article icon

Explain new Context API in React

Last Updated : 09 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

  • In Redux we have to manipulate or update multiple files to add even a single feature but in Context it can be done in much lesser lines of code
  • One way data binding in React is maintained using Context whereas Redux violates it.
  • Multiple stores/contexts can be created using Context whereas Redux creates just a single store

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.

  • First, create a Context.js file in the src folder, where we define the UserContext. The context provides Provider and Consumer, so we'll store them in constants.
  • In a simple component file, we'll display a message showing the user's name and ID, which will be passed through the Provider.
  • Finally, wrap the App component in index.js with the Provider and pass the name and ID as values. If no value is passed, the page will be blank.

Example: Write the following code in respective files

  • Context.js: We create the consumer and provider in this file
  • WelocomePage.js: The consumer consumes the value in this file
  • Index.js: The provider is given to the application in this file
  • App.js: The components are imported in this file and then rendered on the webpage
App.js
import WelcomePage from "./WelcomePage";  function App() {   return (     <div className="App">        <WelcomePage/>     </div>   ); }  export default App; 
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}}>     <App />   </Provider>,   document.getElementById('root') ); 
Context.js
import React from 'react';  const UserContext =React.createContext();  export const Provider = UserContext.Provider; export const Consumer = UserContext.Consumer; 
WelcomePage.js
import React from "react"; import { Consumer } from "./Context";  const WelcomePage = () => {     return (         <div>             <h1>Welcome User :</h1>             <Consumer>                 {(value) => (                     <h2>                         Name: {value.name} id :{value.id}{" "}                     </h2>                 )}                 //this function takes the value as prop             </Consumer>         </div>     ); };  export default WelcomePage; 

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

npm start

Output:


Next Article
Explain new Context API in React

A

aniluom
Improve
Article Tags :
  • Web Technologies
  • ReactJS

Similar Reads

    Context in React
    Context in React is used to share the data through the React Components without passing the props manually for every level of the component tree. It allows the data to be accessed globally throughout the application and enable efficient state management.In this article, you will be introduced to Rea
    4 min read
    Context Hooks in React
    Context Hooks are a feature in React that allows components to consume context values using hooks. Before Hooks, consuming context required wrapping components in Consumer or using a Higher Order Component (HOC). Context Hooks streamline this process by providing a more intuitive and concise way to
    3 min read
    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
    Explain the concept of "lifting up in React Hooks"
    "Lifting State Up" is a concept in React that involves moving the state of a component higher up in the component tree. In React, the state is typically managed within individual components, but there are situations where multiple components need to share and synchronize the same state. When this ha
    2 min read
    React.js Blueprint ContextMenu2 Props
    Blueprint is a React-based UI toolkit for the web. This library is very optimized and popular for building interfaces that are complex and data-dense for desktop applications. In this article, we'll discuss the React.js Blueprint ContextMenu2 Props. Context menus display a list of actions after righ
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences