Passing a value through five components that do not use it, just to reach one that does, is called prop drilling. It makes code hard to change. React Context lets a parent make a value available to everything below it, so any component can read it directly. This guide builds a small theme switcher and then covers when context is the wrong tool.
The three parts
- createContext makes the context and sets a default value.
- Provider wraps part of your tree and supplies the real value.
- useContext reads the value inside any component below the provider.
Step 1: create the context
// ThemeContext.jsx
import { createContext, useContext, useState } from "react";
const ThemeContext = createContext(null);
export function ThemeProvider({ children }) {
const [theme, setTheme] = useState("light");
const toggle = () => setTheme((t) => (t === "light" ? "dark" : "light"));
return (
<ThemeContext.Provider value={{ theme, toggle }}>
{children}
</ThemeContext.Provider>
);
}
export function useTheme() {
const ctx = useContext(ThemeContext);
if (!ctx) throw new Error("useTheme must be used inside ThemeProvider");
return ctx;
}Wrapping useContext in your own hook, here useTheme, keeps the context private to the file and gives a clear error if someone forgets the provider.
Step 2: wrap your app
// main.jsx
import { ThemeProvider } from "./ThemeContext";
root.render(
<ThemeProvider>
<App />
</ThemeProvider>
);Step 3: read it anywhere below
import { useTheme } from "./ThemeContext";
function ThemeButton() {
const { theme, toggle } = useTheme();
return <button onClick={toggle}>Theme: {theme}</button>;
}No props were passed. ThemeButton can sit at any depth under the provider.
A performance point
Every component that reads a context re-renders when the value changes. In the example, the value is a new object on every render of ThemeProvider, so readers re-render each time the provider does. For a small app this is fine. If it becomes a problem, memoise the value:
import { useMemo } from "react";
const value = useMemo(() => ({ theme, toggle }), [theme]);Also split unrelated data into separate contexts, so a change to one does not re-render readers of the other.
When not to use context
- Values that change very often, such as mouse position or a form field being typed into. Keep those in local state.
- Data used by one or two nearby components. Plain props are simpler and easier to follow.
- Server data. A data-fetching library handles caching and refreshing better than context.
Good fits
- The current theme or language.
- The signed-in user.
- A shopping cart used across many pages.
Context or a state library?
For most small and medium apps, state in a few well-placed components plus one or two contexts is enough. Reach for a dedicated library only when you have a lot of shared state with complicated updates. If you are still learning how local state works, read our useState guide first.
Covering financial markets, commodity rates, and practical technology insights for India. Researched and fact-checked in accordance with our Editorial Policy.
Related & Trending Stories
Recommended based on your reading interest
