React useContext Explained for Beginners (No Prop Drilling)
React useContext explained for beginners: learn what context is, how to avoid prop drilling, and share data like a theme between components with simple examples.
React useContext explained for beginners: learn what context is, how to avoid prop drilling, and share data like a theme between components with simple examples.
What is useContext in React? In plain words, useContext lets any component read a shared value directly, without passing that value through every component in between. It solves a problem called prop drilling. In this guide you will learn what context is, why it exists, and how to build a working example step by step.
You do not need to be an expert. If you know what a component is and what props are, you are ready. Every new term is explained the first time we use it.
In React, you pass data from a parent component to a child using props. Props are like function arguments for components. This works well when the child is right below the parent.
But what if a component deep in the tree needs the data? You must pass the props through every component in the middle, even those that do not use them. This is called prop drilling. It is like handing a letter to ten people in a line just so the last person can read it.
function App() {
const user = "Sam";
return <Page user={user} />;
}
function Page({ user }) {
return <Header user={user} />;
}
function Header({ user }) {
return <p>Hello, {user}!</p>;
}Look at Page. It does not use user at all. It only passes it down to Header. With three levels this is fine. With ten levels it becomes messy and hard to change.
Context is a way to share a value with a whole group of components at once. You put the value in one place. Any component inside that group can read it directly, no matter how deep it is.
Think of a radio station. The station broadcasts once. Anyone with a radio can tune in. Nobody needs to pass the music from person to person.
Context has three parts:
A hook is a special React function whose name starts with use. It lets a component use React features. You can read more in our hooks guide linked below.
import { createContext } from "react";
export const ThemeContext = createContext("light");The first line imports createContext from React. The second line creates a new context called ThemeContext. The value "light" inside the brackets is the default. React uses it only when a component reads the context without a Provider above it.
Every context has a Provider component. Anything inside the Provider can read the value you give it.
import { ThemeContext } from "./ThemeContext";
function App() {
return (
<ThemeContext.Provider value="dark">
<Page />
</ThemeContext.Provider>
);
}ThemeContext.Provider wraps the Page component. The value prop is what we share, here the text "dark". Page and every component inside it, at any depth, can now read this value. Notice that we did not pass any props to Page.
import { useContext } from "react";
import { ThemeContext } from "./ThemeContext";
function Header() {
const theme = useContext(ThemeContext);
return <h1 className={theme}>My Website</h1>;
}We import the useContext hook and our ThemeContext. Then we call useContext(ThemeContext). It returns the closest value from a Provider above this component, which is "dark". We store it in a variable called theme and use it as a CSS class name.
Header can sit five levels deep. The components between App and Header do not need to know about the theme.
A fixed value is not very useful. Real apps need to change it. To do that, combine context with state. State is data a component remembers and can change. If you are new to it, read our useState guide first.
We will make a small provider component that holds the state and shares both the value and a function to change it.
import { createContext, useState } from "react";
export const ThemeContext = createContext(null);
export function ThemeProvider({ children }) {
const [theme, setTheme] = useState("light");
function toggleTheme() {
setTheme(theme === "light" ? "dark" : "light");
}
return (
<ThemeContext.Provider value={{ theme, toggleTheme }}>
{children}
</ThemeContext.Provider>
);
}Line by line: ThemeProvider is a normal component. The children prop means whatever you place inside it. useState remembers the current theme, starting at "light". toggleTheme switches between light and dark. The Provider shares an object with two things, the theme and the toggleTheme function. The double curly braces mean a JavaScript object inside JSX.
import { useContext } from "react";
import { ThemeContext } from "./ThemeContext";
function ThemeButton() {
const { theme, toggleTheme } = useContext(ThemeContext);
return (
<button onClick={toggleTheme}>
Current theme: {theme}. Click to switch.
</button>
);
}ThemeButton reads the shared object and picks out theme and toggleTheme using destructuring, which is a short way to pull values out of an object. When you click the button, toggleTheme runs, the state changes, and every component that uses this context updates automatically.
import { ThemeProvider } from "./ThemeContext";
function App() {
return (
<ThemeProvider>
<ThemeButton />
</ThemeProvider>
);
}Finally, App wraps its content in ThemeProvider. Now ThemeButton, and any other component inside, can use the theme.
| Situation | Best choice |
|---|---|
| Passing data to a direct child | Props |
| Data needed by one or two nearby components | Props or lifting state up |
| Data needed by many components at different depths | Context |
| Global things like theme, language, or logged-in user | Context |
Props are simple and easy to follow, so start with them. Reach for context only when prop drilling becomes painful. Do not use context for everything. It makes it harder to see where data comes from.
New to data flow in React? Read our guide on lifting state up first. It shows the simpler way to share data between nearby components.
Read: Lifting State UpNot sure how props differ from state? This short guide makes it clear.
Read: Props vs StateuseContext lets a component read a shared value from a Provider above it. It is used for data many components need, like a theme, language, or the logged-in user. It helps you avoid passing props through many layers.
Prop drilling is passing props through several components that do not use them, just so a deeply nested component can receive them. It makes code harder to read and change. Context is one common way to avoid it.
No. useContext is built into React and simply shares a value. Redux is a separate library with more tools for managing complex app state. Beginners usually only need useContext, if anything.
Yes. You can create as many contexts as you like, for example one for theme and one for user. A component can call useContext several times to read each one.
Avoid it when data is only needed by a parent and its direct children. Plain props are simpler there. Context is best for values that many distant components need.
Want help building a React project or learning web development step by step? Get in touch with the Fepiq team.
Contact FepiqOccasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.
Keep reading
Learn SQL AND, OR and NOT operators with simple examples. See how to combine conditions in WHERE and avoid the common mistakes beginners make.
Learn JavaScript template literals with simple examples: backticks, ${} variables, and multi-line strings. A beginner-friendly guide you can copy and run.
Let's build something
Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.