useContext

The React hook that reads the value of a context from the nearest provider above the component.

In more detail

The component subscribes to that context and re-renders whenever the provider value changes, even if it uses only part of it. Unlike use, useContext must follow the rules of hooks and cannot be called inside a condition.

Examples

Tabs triggers call useContext(Ctx) to learn which tab is active; a custom hook wrapping it should throw if no provider is found.
const { mode } = useContext(ThemeContext);

Related terms

Taught in

References