Skip to content

Context

In one line

Context is a React feature for passing data through the component tree without manually passing props at every level.

In simple words

Context provides a way to share values (themes, current user, locale, settings) across the entire component tree without prop drilling. You create a Context with createContext(defaultValue), provide a value with <Context.Provider value={...}>, and consume it with useContext(Context) or <Context.Consumer>.

The Provider determines the value; all descendant consumers read from the nearest Provider above them. When the Provider's value changes, all consumers re-render.

Context is best for low-frequency, app-wide data. For high-frequency updates, Context can cause performance issues because every consumer re-renders on change. In those cases, consider splitting Contexts or using a state management library.

Example

jsx
const UserContext = createContext(null);

function App() {
  const [user, setUser] = useState({ name: "Alice" });
  return (
    <UserContext.Provider value={user}>
      <Profile />
    </UserContext.Provider>
  );
}

function Profile() {
  const user = useContext(UserContext);
  return <h1>{user.name}</h1>;
}

App provides the user via Context. Profile reads it without receiving it as a prop, no matter how deep it is in the tree.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary