Understanding State Management in React: A Deep Dive into Context API

Meghna ChandraMeghna Chandra
5 min read

In every React Application, the state determines how components behave, how data flows and how your UI responds to user actions.

In small apps, managing state locally within components is simple and usually enough. But as your app grows, with more components needing access to the same data, things get messy. You might find yourself passing through same data through several layers of components, even if some of them don’t actually need it. This is called prop drilling.

Prop drilling means passing props down through multiple levels of components, just so a deeply nested child can use them. This makes your code harder to read, maintain and scale.
To solve this problem, React offers a built-in tool: the Context API. It allows you to share state and functions directly with the components that need them, without passing props manually at every level.

In this article, we’ll explore what the Context API is, why it’s useful and how to use it to manage shared state in a clean and simple way.


What is State Management in React?

Let’s first understand what state is and why we need to manage it in React.

State refers to data that changes over time — like a user’s input, whether a modal is open, or the current theme of your app.

Think of it like this:
If your app were a person, the state would be like their current mood, thoughts, or energy level, things that change depending on what’s happening.

In React:
- Clicking a button
- Typing into an input
- Fetching data from an API
all of these can change the state, which then updates the UI automatically

Managing this state means keeping track of it and making sure the right components update when it changes.

There are two types of state in React:

  • Local State: Managed within a single component using tools like useState. It’s perfect for things like form inputs, toggles, or small pieces of UI that don’t affect the rest of your app.

  • Global State: Shared across multiple components. This is needed when several parts of your app need access to the same data — like user information, authentication status, or theme settings.

As your app grows, managing global state becomes more important and more challenging. Without a proper solution, you may end up with prop drilling, duplicate state logic, or tightly coupled components.

That’s where state management tools like the Context API or Redux come in. They help you keep shared state in one place and make it available to any component that needs it without cluttering your code.


The Problem: Prop Drilling

Here’s a simple example that shows what prop drilling looks like:

function App() {
  const theme = "light";
  return <Parent theme={theme} />;
}

function Parent({ theme }) {
  return <Child theme={theme} />;
}

function Child({ theme }) {
  return <GrandChild theme={theme} />;
}

function GrandChild({ theme }) {
  return <div>The theme is {theme}</div>;
}

In this example:

  • Only Grandchild needs the theme.

  • But we have to pass it through Parent and Child just to get it there.

This can become messy and repetitive — especially if your app has many levels or a lot of shared data.


Enter the Context API

Instead of manually passing data through each component, the Context API allows you to share data across components — no matter how deeply nested they are — without passing props at every level.

You can think of it like this:

You set up a central speaker (the context) that announces the data, and any component that wants to “listen” can tune in directly — no middlemen needed.

How Does It Work?

Using the Context API involves 3 main steps:

  1. Create a Context
    This creates a place to store your shared data.

     const ThemeContext = React.createContext();
    
  2. Provide the Data
    Wrap your components with a <Provider> and give it the value you want to share.

     <ThemeContext.Provider value="light">
       <App />
     </ThemeContext.Provider>
    
  3. Consume the Data
    Any component inside the provider can access the data using useContext.

     const theme = React.useContext(ThemeContext);
    

    That’s it! You’ve now shared state across components without drilling props through every layer.

    Diagram: Prop Drilling vs Context API


Theme Toggle App

Let’s walk through a practical example when we toggle between light and dark themes using Context API

//create the context
const ThemeContext = React.createContext();

//create a provider component
function ThemeProvider({ children }) {
    const [theme, setTheme] = React.useState("light");

    const toggleTheme = () => {
    setTheme(prev => (prev === "light" ? "dark" : "light")
    };

    const value = { theme, toggleTheme };

    return (
    <ThemeContext.Provider value={value}>
        {children}
    </ThemeContext.Provider>
    );
}

//create components that consume the context
function ThemeSwitcher() {
    const { theme, toggleTheme } = React.useContext(ThemeContext);
    return (
    <button onClick={toggleTheme}>
        Current theme: {theme} (Click to toggle)
    </button>
    )
}

function App() {
    return (
        <ThemeProvider>
            <ThemeSwitcher />
        </ThemeProvider>
    );
}

This app keeps the theme value in one place and lets any component access or update it — no prop drilling needed.


Pros/Cons of Context API

Pros:

  • Built into React (no external libraries)

  • Reduces prop drilling

  • Simple and easy to implement for small to medium apps

Cons:

  • Not optimized for high-frequency updates (e.g., typing input fields)

  • Can lead to unnecessary re-renders if not used carefully


Conclusion

The Context API provides a powerful and elegant solution for state management in React when you want to share data across components without prop drilling. While it may not replace Redux for every use case, it's a great choice for many apps that need simplicity, clarity, and ease of use.

0
Subscribe to my newsletter

Read articles from Meghna Chandra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Meghna Chandra
Meghna Chandra