All posts
ReactNovember 17, 20268 min read

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.

F
Fepiq Team
Fepiq

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.

The problem: what is prop drilling?

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.

jsx
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.

What is context in React?

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:

  1. createContext: creates the context (the radio channel).
  2. Provider: a component that broadcasts a value to everything inside it.
  3. useContext: a hook that lets a component tune in and read the value.

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.

Step 1: Create the context

jsx
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.

Step 2: Wrap components in a Provider

Every context has a Provider component. Anything inside the Provider can read the value you give it.

jsx
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.

Step 3: Read the value with useContext

jsx
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 complete example: a theme toggle

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.

jsx
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.

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.

jsx
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.

Props vs context: which should you use?

SituationBest choice
Passing data to a direct childProps
Data needed by one or two nearby componentsProps or lifting state up
Data needed by many components at different depthsContext
Global things like theme, language, or logged-in userContext

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.

Common beginner mistakes

  • Forgetting the Provider. If no Provider is above your component, you only get the default value, and your toggle will seem broken.
  • Using useContext outside a component. Hooks only work inside function components.
  • Putting too much in one context. Keep contexts small, such as one for theme and another for the user.
  • Importing the wrong context. Make sure the same ThemeContext object is used in both the Provider and useContext.

Quick recap

  • Prop drilling means passing props through components that do not need them.
  • Context shares a value with many components at once.
  • createContext makes it, Provider broadcasts it, useContext reads it.
  • Combine context with useState to share values that change.

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 Up

Not sure how props differ from state? This short guide makes it clear.

Read: Props vs State

Frequently asked questions

What is useContext in React used for?+

useContext 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.

What is prop drilling in React?+

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.

Is useContext the same as Redux?+

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.

Can I use more than one context in the same app?+

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.

When should I not use context?+

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 Fepiq

Get new posts in your inbox

Occasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.

Keep reading

Related posts

All posts

Let's build something

Ready to ship your next product with Fepiq?

Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.