Home / Learn React / Context: Share Data Without Props Drilling
Context: Share Data Without Props Drilling
Passing props through many layers just to reach a deep child is tiring. Context lets a parent provide a value that any component below it can read directly.
Theory in plain words
What it is
Context is a way to make a value available to a whole subtree of components. useContext(MyContext) reads the nearest value provided above.
Why we use it
Some data is needed everywhere: the current user, the theme, the language. Passing it as a prop through ten components ("prop drilling") makes code noisy and fragile.
How it works
You create a context with createContext(default). A parent wraps its children in the context provider with a value. Any descendant that calls useContext gets that value and re-renders when it changes.
Where it is used
- Theme (light and dark)
- Logged in user and permissions
- Language and translations
- Shopping cart and app settings (often with useReducer)
Theme example
In React 19 you can use the context itself as the provider: <ThemeContext value=...>. Older code writes <ThemeContext.Provider value=...>, which still works.
A user context with a custom hook
- Every component that reads a context re-renders when its value changes. Split big contexts (one for the user, one for the theme).
- If the value is a new object every render (
value={{ a, b }}) all readers re-render each time. Memoize withuseMemowhen it matters. - Context is not a full state manager. For large shared state think about Zustand, Redux Toolkit or TanStack Query.
children) before reaching for context.Quick quiz
1. What problem does context solve?
Deep children read the value directly.
2. Which hook reads a context?
useContext(MyContext).
3. When a context value changes, which components re-render?
Only readers (and the provider tree parts that re-render for other reasons).
Common questions
What problem does context solve?
Prop drilling through many layers. Deep children read the value directly.
Which hook reads a context?
useContext. useContext(MyContext).
When a context value changes, which components re-render?
Every component that reads that context. Only readers (and the provider tree parts that re-render for other reasons).