React Tutorial 0/120 lessons ~6 min read Lesson 63

    Context API

    Context API covers providers and consumers, value identity, re-renders, splitting contexts, and when Context is not a Redux replacement.

    Course progress0%
    Focus
    11 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Course: React — State Management Lesson: 03

    Learning objectives

    Explain React Context, create and consume a context, understand provider scope, and recognize when Context is appropriate.

    The problem Context solves

    Props are the normal way to pass data through a component tree. When many intermediate components must pass a value they do not use, prop drilling can make the tree noisy.

    Context allows a parent to make a value available to descendants without forwarding it manually through every intermediate component.

    text
    App
    |
    +-- AuthProvider
    |
    +-- Header
    | +-- UserMenu
    |
    +-- Workspace
    +-- Dashboard

    Create a context

    javascript
    import { createContext, useContext, useState } from "react";
    const ThemeContext = createContext(null);
    function ThemeProvider({ children }) {
    const [theme, setTheme] = useState("light");
    const value = { theme, setTheme };
    return (
    <ThemeContext.Provider value={value}>
    {children}
    </ThemeContext.Provider>
    );
    }
    function useTheme() {
    const context = useContext(ThemeContext);
    if (context === null) {
    throw new Error("useTheme must be used within ThemeProvider");
    }
    return context;
    }

    The provider owns the value. Descendants call useContext through a custom Hook.

    Consume context

    javascript
    function ThemeButton() {
    const { theme, setTheme } = useTheme();
    return (
    <button onClick={() =>
    setTheme(current => current === "light" ? "dark" : "light")
    }>
    Current theme: {theme}
    </button>
    );
    }

    The component does not need to know how the provider stores or updates the theme.

    Provider scope

    A context value is available to descendants of its provider. A component outside that provider receives the context's default value. Nested providers can intentionally override a value for a subtree.

    Provider placement is an architectural decision. Place a provider high enough for all intended consumers, but avoid wrapping the entire application in every feature-specific provider.

    Context and rendering

    When a provider's value changes, consumers that read that context are notified and may re-render. Avoid putting unrelated, frequently changing values into one large context object. Split contexts by responsibility when it improves ownership and update behavior.

    Context is value distribution, not a complete data platform

    Context does not automatically provide server caching, request deduplication, persistence, selectors, or sophisticated update policies. It can be combined with reducers or a store, but should not be treated as a universal replacement for every state-management tool.

    Common mistakes

    • Using context for values needed by only one component.
    • Creating a context without a useful default or provider guard.
    • Putting all application state in one context.
    • Recreating large provider values unnecessarily.
    • Assuming Context automatically solves server-state caching.

    Best practices

    Create focused contexts, expose them through custom Hooks, define provider scope intentionally, and measure rendering behavior before adding memoization.

    Interview questions & answers

    Interview preparation

    5 questions grouped by difficulty — expand any card for a model answer and follow-up probe.

    Beginner

    2
    1. 1What problem does Context solve?
      Beginner

      Model answer

      It lets you pass values deeply without threading props through every intermediate component.

    2. 2Is Context a replacement for Redux or a server-state library?
      Beginner

      Model answer

      No.

      Context distributes values; Redux/stores manage complex client transitions; server-state libraries manage remote cache lifecycles.

    Intermediate

    2
    1. 3What happens when a provider value changes?
      Intermediate

      Model answer

      Consumers that read that context re-render.

      Unstable value identity can cause more renders than expected.

    2. 4How would you split a large application context?
      Intermediate

      Model answer

      Split by concern—auth, theme, feature data—so unrelated updates do not force every consumer to re-render.

    Advanced

    1
    1. 5Can a component consume a context outside its provider?
      Advanced

      Model answer

      It receives the default value, which is often wrong for required dependencies—custom Hooks usually throw if the provider is missing.

    Practice

    Create separate ThemeContext and AuthContext providers. Consume them in the header and dashboard, and test behavior when a consumer is rendered without its provider.

    Ready to mark this lesson complete?Track your journey across the entire course.