codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚛️

React.js

18 / 61 topics
18Context API Basics19Hooks: Introduction and Overview20Using the useState Hook21Using the useEffect Hook22Creating Custom Hooks23Memoization in React
Tutorials/React.js/Context API
⚛️React.js

Context API

Updated 2026-05-15
10 min read

Context API

Introduction

In React applications, managing state can become complex as your application grows. One common approach to manage state across multiple components is using the Context API. The Context API allows you to share values between components without having to explicitly pass a prop through every level of the tree.

This tutorial will guide you through understanding and implementing the Context API for global state management in React applications.

Concept

The Context API provides a way to pass data through the component tree without having to pass props down manually at every level. This is particularly useful for themes, user authentication status, or any other global state that needs to be accessible by many components at different nesting levels.

Key Concepts

  1. Context Provider: A React component that allows you to provide a value to all its children and grandchildren.
  2. Context Consumer: A way for child components to subscribe to context changes.
  3. useContext Hook: A hook that lets you subscribe to React context without introducing nesting.

Examples

Example 1: Basic Context Usage

Let's create a simple example where we manage the theme of an application using the Context API.

Step 1: Create a Context

First, we need to create a context for our theme.

jsx
1import React from 'react';
2
3const ThemeContext = React.createContext('light');
4
5export default ThemeContext;

Step 2: Provide the Context Value

Next, we'll wrap our application in a ThemeContext.Provider to provide the context value.

jsx
1import React from 'react';
2import ReactDOM from 'react-dom';
3import ThemeContext from './ThemeContext';
4import App from './App';
5
6ReactDOM.render(
7<ThemeContext.Provider value="dark">
8 <App />
9</ThemeContext.Provider>,
10document.getElementById('root')
11);

Step 3: Consume the Context Value

Now, we can consume the context value in any child component using ThemeContext.Consumer.

jsx
1import React from 'react';
2import ThemeContext from './ThemeContext';
3
4const ThemedButton = () => (
5<ThemeContext.Consumer>
6 {theme => (
7 <button style={{ background: theme === 'dark' ? '#333' : '#fff', color: theme === 'dark' ? '#fff' : '#000' }}>
8 I am styled by theme context!
9 </button>
10 )}
11</ThemeContext.Consumer>
12);
13
14export default ThemedButton;

Example 2: Using useContext Hook

The useContext hook provides a simpler way to consume context values, especially in functional components.

Step 1: Modify the Consumer Component

Let's refactor the previous example to use the useContext hook.

jsx
1import React, { useContext } from 'react';
2import ThemeContext from './ThemeContext';
3
4const ThemedButton = () => {
5const theme = useContext(ThemeContext);
6return (
7 <button style={{ background: theme === 'dark' ? '#333' : '#fff', color: theme === 'dark' ? '#fff' : '#000' }}>
8 I am styled by theme context!
9 </button>
10);
11};
12
13export default ThemedButton;

Example 3: Updating Context Value

To update the context value, you need to use a state variable in your provider component.

Step 1: Create a Toggle Theme Component

Let's create a component that toggles the theme.

jsx
1import React, { useState } from 'react';
2import ThemeContext from './ThemeContext';
3
4const ThemeToggle = () => {
5const [theme, setTheme] = useState('light');
6
7const toggleTheme = () => {
8 setTheme(theme === 'dark' ? 'light' : 'dark');
9};
10
11return (
12 <div>
13 <button onClick={toggleTheme}>
14 Toggle Theme
15 </button>
16 <ThemeContext.Provider value={theme}>
17 {/* Child components go here */}
18 </ThemeContext.Provider>
19 </div>
20);
21};
22
23export default ThemeToggle;

What's Next?

In this tutorial, you learned how to use the Context API for global state management in React. The next step is to explore more advanced state management solutions like Redux, which provides a more robust and scalable approach to managing application state.

Stay tuned for the "Introduction to Redux" section where we'll dive deeper into using Redux for complex applications.


PreviousLifting State UpNext Hooks: Introduction and Overview

Recommended Gear

Lifting State UpHooks: Introduction and Overview