useContext in React

From the React Hooks cheat sheet · Context & Refs · verified Jul 2026

useContext

Consume context values

javascript
// Create context
const ThemeContext = createContext()

// Provider component
<ThemeContext.Provider value={{ dark: true }}>
  <App />
</ThemeContext.Provider>

// React 19: render <Context> directly as the provider
<ThemeContext value={{ dark: true }}>
  <App />
</ThemeContext>

// Consume in any child
const theme = useContext(ThemeContext)

// React 19: use() can read context conditionally (inside if/loops)
const theme2 = use(ThemeContext)

// Custom hook pattern
function useTheme() {
  const context = useContext(ThemeContext)
  if (!context) throw new Error('No ThemeProvider')
  return context
}
💡 Component re-renders when context value changes
⚡ React 19: render <Context value={}> directly (no .Provider); use(Context) reads conditionally
✅ Better than prop drilling for deep trees

Continue with React Hooks

Save the full cheat sheet or work through every related task.

More React tasks

Back to the full React Hooks cheat sheet