MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Creating and Consuming React Context with useContext0%
Study Materials
React.js for IntermediateProgress 0%
1.1Component Reusability and Polymorphic Components1.2Render Props Pattern in Modern React1.3Higher-Order Components vs Modern Hooks1.4Compound Components Pattern Explained
5.1The Prop Drilling Dilemma and Context Solution5.2Creating and Consuming React Context with useContext5.3Building Custom Context Providers with State5.4Context Performance and Preventing Render Cascades
HomeTutorialsReact.js for IntermediateCreating and Consuming React Context with useContext
Building Custom Context Providers with State

Creating and Consuming React Context with useContext

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

1. The 3 Steps of Context

Working with React Context involves three distinct, sequential steps:

  1. 1
    Create the Context: Call createContext() to define the data conduit and optional default fallback.
  2. 2
    Provide the Context: Wrap the component subtree with <Context.Provider value={...}>.
  3. 3
    Consume the Context: Read the provided value inside any descendant component using useContext(Context).
1[1. createContext()] ──> [2. <Context.Provider value={val}>] ──> [3. useContext(Context)]

2. Step 1: Creating the Context

In a dedicated module file (e.g., src/context/ThemeContext.js), create and export your context:

1// src/context/ThemeContext.js
2import { createContext } from 'react';
3 
4// Create context with an optional default value
5export const ThemeContext = createContext('light');

3. Step 2: Providing the Context

In a parent or top-level component, import the context and wrap your tree with ThemeContext.Provider:

1// src/App.jsx
2import React, { useState } from 'react';
3import { ThemeContext } from './context/ThemeContext';
4import Header from './components/Header';
5import CourseDashboard from './components/CourseDashboard';
6 
7export default function App() {
8 const [theme, setTheme] = useState('dark');
9 
10 const toggleTheme = () => {
11 setTheme(prev => (prev === 'light' ? 'dark' : 'light'));
12 };
13 
14 return (
15 // Pass both current theme and updater function in context value
16 <ThemeContext.Provider value={{ theme, toggleTheme }}>
17 <div className={`app-root theme-${theme}`}>
18 <Header />
19 <CourseDashboard />
20 </div>
21 </ThemeContext.Provider>
22 );
23}

4. Step 3: Consuming the Context with useContext

In any deeply nested child component, import the context object and invoke the useContext hook:

1// src/components/ThemeToggleButton.jsx
2import React, { useContext } from 'react';
3import { ThemeContext } from '../context/ThemeContext';
4 
5export default function ThemeToggleButton() {
6 // Directly consume context without prop drilling!
7 const { theme, toggleTheme } = useContext(ThemeContext);
8 
9 return (
10 <button
11 onClick={toggleTheme}
12 className={`theme-toggle-btn ${theme === 'dark' ? 'btn-dark' : 'btn-light'}`}
13 >
14 Current Theme: {theme.toUpperCase()} (Click to toggle)
15 </button>
16 );
17}

5. How useContext Triggers Re-Renders

Whenever the value passed into <ThemeContext.Provider value={...}> changes:

  • React automatically re-renders every component that calls useContext(ThemeContext).
  • This update bypasses intermediate components, even if those intermediate components are wrapped in React.memo!

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What hook is used to consume a React Context in a functional component?
Question 2
What must be passed as an argument to useContext()?
Question 3
What component must wrap the hierarchy for descendants to receive custom context values?
Question 4
If the context value updates, which components will React re-render?
Question 5
What does createContext(defaultValue) return?
Next Lesson

Building Custom Context Providers with State

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
The Prop Drilling Dilemma and Context SolutionBuilding Custom Context Providers with State

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master React.js for Intermediate with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext