The useContext() hook is the modern, convenient way to read shared data from a React context inside any function component. It replaces the older Context.Consumer pattern and keeps your components clean and readable.
Think of useContext() as a direct portal to global data like theme, language, or logged-in user — without manually passing props down every level.
✨ Best for: Global / shared state in function components
useContext(Context) reads the current value from the nearest matching Provider.useContext() automatically re-render when the context value changes.useContext() inside React function components or custom hooks.The basic syntax of useContext() looks like this:
// Read the current value of a context
const value = useContext(MyContext);
MyContext is the context object created using createContext().value will be whatever you passed to the matching <MyContext.Provider value={...} />.createContext(defaultValue) is used.Let’s build a simple ThemeContext to share the current theme and a function to toggle it.
// ThemeContext.js - defines a shared theme context
import { createContext } from "react";
const ThemeContext = createContext("light");
export default ThemeContext;
// 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 - reads and updates theme using useContext
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;
Initial UI:
Current Theme: lightAfter clicking “Toggle Theme”:
Current Theme: darkBoth approaches read from context, but useContext() is cleaner in modern React apps.
| Aspect | useContext Hook | Context.Consumer |
|---|---|---|
| Syntax | Simple hook call | Render-prop function |
| Code Length | Short & clean | Verbose & nested |
| Hooks Support | ✅ Works in function components | ✅ Works in class & function (but older style) |
| Performance | Efficient, fewer nested components | Can cause extra nesting |
| Preferred Usage | Modern React apps (Hooks) | Legacy / pre-Hooks codebases |
You can combine multiple contexts by calling useContext() several times within the same component. This is useful when you want to share theme, authentication, language, etc. together.
// Dashboard.js - consuming multiple contexts
import React, { useContext } from "react";
import ThemeContext from "./ThemeContext";
import AuthContext from "./AuthContext";
function Dashboard() {
const { theme } = useContext(ThemeContext);
const { user } = useContext(AuthContext);
return (
<div className={`p-3 bg-${theme}`}>
<h4>Welcome, {user ? user.name : "Guest"}!</h4>
</div>
);
}
export default Dashboard;
ThemeContext controls the background appearance.AuthContext provides the current user object."Guest".useContext() only at the top level of your component or custom hook.useContext() inside loops, conditions, or nested functions.useContext() outside a matching Provider — it will fall back to the default value.| Concept | Description |
|---|---|
useContext(Context) |
Returns the current value of a context |
| Re-renders | Triggers when the Provider's value changes |
| Usage | Best suited for React function components |
| Replaces | Most uses of older Context.Consumer syntax |
useTheme()) for cleaner components.LanguageContext that stores { language, setLanguage }.<LanguageContext.Provider> and default the language to "English".Header component that uses useContext(LanguageContext) to show the current language.Header to toggle between "English" and "Hindi".Goal: Practice using useContext() for easy, clean access to shared global data in React components.
// Starter idea for LanguageContext practice
import React, { createContext, useState, useContext } from "react";
const LanguageContext = createContext({ language: "English", setLanguage: () => {} });
function LanguageProvider({ children }) {
const [language, setLanguage] = useState("English");
return (
<LanguageContext.Provider value={{ language, setLanguage }}>
{children}
</LanguageContext.Provider>
);
}
function Header() {
const { language, setLanguage } = useContext(LanguageContext);
return (
<header>
<p>Current language: {language}</p>
<button onClick={() => setLanguage(language === "English" ? "Hindi" : "English")}>
Toggle Language
</button>
</header>
);
}
export { LanguageProvider, LanguageContext, Header };