The Context API in React provides a way to share data across the component tree without passing props manually through every nested level.
It is perfect for global data such as:
? No More Prop Drilling ? Global State Sharing
The Context API mainly revolves around three core steps:
React.createContext()Provider componentuseContext() or Context.ConsumerImportant pieces:
createContext() → Creates a new context object.Provider → Makes a value available to all children.useContext() → Reads the current context value.Consumer → Older way to use context (render prop pattern).Basic pattern when working with Context API in a React app:
// Create a context with an optional default value
import React from "react";
export const MyContext = React.createContext("defaultValue");
// Wrap part of your tree with the Provider
import React from "react";
import { MyContext } from "./MyContext";
import SomeChild from "./SomeChild";
function App() {
const sharedValue = "Hello from Context!";
return (
<MyContext.Provider value={sharedValue}>
<SomeChild />
</MyContext.Provider>
);
}
export default App;
// Read the nearest context value with useContext
import React, { useContext } from "react";
import { MyContext } from "./MyContext";
function SomeChild() {
const value = useContext(MyContext);
return <p>Current value: {value}</p>;
}
export default SomeChild;
✨ useContext = Fast Access to Context Data
In this example, we use the Context API to store and toggle the app’s theme between "light" and "dark".
// ThemeContext.js - defines a context for theme state
import React from "react";
export const ThemeContext = React.createContext("light");
// App.js - wraps children with ThemeContext.Provider
import React, { useState } from "react";
import { ThemeContext } from "./ThemeContext";
import Toolbar from "./Toolbar";
function App() {
const [theme, setTheme] = useState("light");
return (
<ThemeContext.Provider value={{ theme, setTheme }}>
<Toolbar />
</ThemeContext.Provider>
);
}
export default App;
// Toolbar.js - uses useContext to read & update theme
import React, { useContext } from "react";
import { ThemeContext } from "./ThemeContext";
function Toolbar() {
const { theme, setTheme } = useContext(ThemeContext);
return (
<div
className={`p-3 text-center bg-${
theme === "light" ? "light" : "dark"
} text-${theme === "light" ? "dark" : "light"}`}
>
<p>Current Theme: {theme}</p>
<button
className="btn btn-primary"
onClick={() => setTheme(theme === "light" ? "dark" : "light")}
>
Toggle Theme
</button>
</div>
);
}
export default Toolbar;
"light").Toolbar reads theme and setTheme from ThemeContext.Here, the Context API is used to manage login state and functions like login() and logout() globally.
// AuthContext.js - manages global authentication state
import React, { createContext, useState } from "react";
export const AuthContext = createContext();
export function AuthProvider({ children }) {
const [user, setUser] = useState(null);
const login = (username) => setUser({ name: username });
const logout = () => setUser(null);
return (
<AuthContext.Provider value={{ user, login, logout }}>
{children}
</AuthContext.Provider>
);
}
// App.js - provides auth context to the whole app
import React from "react";
import { AuthProvider } from "./AuthContext";
import Dashboard from "./Dashboard";
function App() {
return (
<AuthProvider>
<Dashboard />
</AuthProvider>
);
}
export default App;
// Dashboard.js - consumes auth state & actions
import React, { useContext } from "react";
import { AuthContext } from "./AuthContext";
function Dashboard() {
const { user, login, logout } = useContext(AuthContext);
return (
<div className="text-center">
{user ? (
<>
<h4>Welcome, {user.name}!</h4>
<button className="btn btn-danger" onClick={logout}>
Logout
</button>
</>
) : (
<button
className="btn btn-success"
onClick={() => login("Meghraj")}
>
Login
</button>
)}
</div>
);
}
export default Dashboard;
user is null, so the Login button is shown.login("Meghraj") sets a user object in context.user, login and logout without prop drilling.The useContext() hook is the most common and cleaner way to consume context data in modern React applications.
// Simple pattern to read context value
import React, { useContext } from "react";
import { MyContext } from "./MyContext";
function MyComponent() {
const value = useContext(MyContext);
return <p>Context value: {value}</p>;
}
useContext(MyContext) always returns the nearest <MyContext.Provider> value above it in the component tree.
| Concept | Description |
|---|---|
createContext() |
Creates a new Context object. |
Provider |
Supplies the context value to all its children. |
useContext() |
Hook that lets components consume context easily. |
Consumer |
Older render-prop API (less common now). |
useReducer for more complex or scalable state management.LanguageContext that toggles between English and Hindi and displays text in the chosen language.ThemeContext that stores "dark" or "light" and toggles theme using a button.UserContext that tracks login status and shows the current user’s name in a navbar.AuthContext + ThemeContext + LanguageContext).Goal: Understand how the Context API simplifies data sharing across components, removes prop drilling, and improves scalability in medium-to-large React applications.