Skip to content

useContext

In one line

useContext is a Hook that lets a component read and subscribe to a Context value without manually passing props through every level.

In simple words

useContext solves the problem of prop drilling — passing data through many layers of components that don't need it themselves, just to reach a deeply nested child.

First you create a Context with React.createContext(defaultValue). A parent provides the value with <MyContext.Provider value={...}>. Any descendant, no matter how deep, can read it with const value = useContext(MyContext).

When the Provider's value changes, all components calling useContext with that context re-render. This makes Context great for themes, user authentication, locale, and other app-wide data. For frequently changing values, be aware that all consumers re-render on every change.

Example

jsx
const ThemeContext = createContext("light");

function App() {
  return (
    <ThemeContext.Provider value="dark">
      <Toolbar />
    </ThemeContext.Provider>
  );
}

function Toolbar() {
  const theme = useContext(ThemeContext);
  return <button className={theme}>Click</button>;
}

Toolbar reads the theme directly from Context without receiving it as a prop. The Provider in App supplies the value.

Related terms

Related Resources

Related Lessons

  • useContext

    Learn Context and useContext for global data.

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