Skip to content
IT glossary
FrontendUpdated December 3, 2026

React Context

React lets nested components read a shared value, such as a theme, without passing it as a prop through every level. A function component can read that value with the useContext hook.

Also known as: useContext

What problem does it solve?

Suppose an account menu needs the current language, but the header and layout above it don't. Passing the language through each layer makes those components accept props they only forward. That's called prop drilling. With React Context, a provider supplies the language to a branch of the component tree, and the menu reads it where it's needed.

Context is the mechanism for sharing the value. useContext is the hook a function component calls to read it and receive updates.

How React picks the value

Call createContext(defaultValue) outside your components to create a context object. Put a provider above the components that need the value. A component that calls useContext gets the value from the closest provider for that same context object above it in the React tree. A nested provider can override the value for just one branch.

The default is a fallback for when no matching provider exists. A matching provider with value={undefined} supplies undefined, not the default. Also, a provider returned by a component can't supply the value to a useContext call in that same component: the provider has to be above the reader.

Example: a different theme for a preview

import { createContext, useContext } from 'react';

const ThemeContext = createContext('light');

function SaveButton() {
  const theme = useContext(ThemeContext);
  return <button className={`button-${theme}`}>Save</button>;
}

export default function Preview() {
  return (
    <ThemeContext.Provider value="dark">
      <section><SaveButton /></section>
    </ThemeContext.Provider>
  );
}

The preview's button gets dark. Render the same button without a theme provider above it and it gets light. The example uses the .Provider syntax available in React 18 and 19. In React 19, you can also write <ThemeContext value="dark"> directly.

When to use it, and what to watch for

Context works well for settings that many components in a branch need, such as theme or locale. For a direct child, a prop is often the better choice: readers can see what the child depends on at the call site. Context doesn't hold or update state by itself. For a theme switcher, keep the current theme in useState and pass the state and update function through the provider.

React compares the previous and next provider values with Object.is. An inline object such as value={{ theme, setTheme }} is a new value every time the provider's owner renders, even if the theme hasn't changed. When the value changes, components reading that context re-render even if they're wrapped in React.memo. If this causes a performance problem, stabilize the object with useMemo or split independently changing values into separate contexts. Reading just one field from a context object does not subscribe you to only that field.

In an interview

If asked whether Context replaces Redux, separate distribution from state management. Context makes a value available and notifies readers when it changes. Pairing it with useState or useReducer can be enough for shared state. A dedicated store may be a better fit when consumers need to subscribe to selected slices independently or the team needs middleware and state debugging tools.