Skip to main content

What does `useContext()` do?

What useContext() does

The useContext() hook lets you read a value from React context that was passed down through a <Context.Provider> component somewhere higher up the tree.

More simply: useContext lets a component reach shared data without passing it through props.


How it works (step by step)

1. Create a context

javascript
import { createContext } from "react"; export const ThemeContext = createContext("light");

createContext() creates a context object with a .Provider and a .Consumer method.


2. Wrap part of the app in a Provider

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

Everything inside <ThemeContext.Provider> gets access to the value "dark".


3. Read the context value in any nested component through useContext

javascript
import { useContext } from "react"; import { ThemeContext } from "./ThemeContext"; function Button() { const theme = useContext(ThemeContext); return <button className={theme}>Theme: {theme}</button>; }

Now Button knows which theme is selected (dark), even though nobody explicitly passed it that prop!


Example using state

Context is often combined with state to pass dynamic data (for example, theme, language, or the user).

javascript
const ThemeContext = createContext(); function App() { const [theme, setTheme] = useState("light"); return ( <ThemeContext.Provider value={{ theme, setTheme }}> <Toolbar /> </ThemeContext.Provider> ); } function Toolbar() { const { theme, setTheme } = useContext(ThemeContext); return ( <button onClick={() => setTheme(theme === "light" ? "dark" : "light")}> Toggle theme (current: {theme}) </button> ); }

Now any number of components can read and change the theme, without threading props through every level.


When to use useContext

WhenWhy
Data is needed in many componentsFor example, the current user, language, theme, settings
You do not want to pass props by handuseContext removes "prop drilling"
You need a global "state" without Redux or ZustandContext + useState = simple global logic

Important to know

FeatureExplanation
useContext re-renders a component when the context value changesIf value changed, every subscriber re-renders
Context is not cached like useMemoUpdates are delivered synchronously
Context cannot be called outside a ProviderOtherwise the value comes from createContext(defaultValue)
Contexts can be nestedReact merges them correctly

Difference from props

PropsuseContext
Passed top-down manuallyAutomatically available to all descendants
Work locallyWork globally within the Provider
More flexible and transparentMore convenient for global data

Summary

QuestionAnswer
What does useContext() doLets you access the value from the nearest Context.Provider
When to use itWhen components need to share common data without props
What it returnsThe current context value
Usage examplesTheme, language, user, settings, authentication state

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.