Context API in React

From the React Components & JSX cheat sheet ยท Context API ยท verified Jul 2026

Context API

Create, provide, and consume context with createContext and useContext

jsx
import { createContext, useContext, useState } from 'react';

const ThemeContext = createContext('light');

function App() {
  const [theme, setTheme] = useState('dark');
  return (
    <ThemeContext.Provider value={theme}>
      <Toolbar />
    </ThemeContext.Provider>
  );
}

function Toolbar() {
  const theme = useContext(ThemeContext);
  return <div className={theme}>Toolbar</div>;
}
๐Ÿ’ก Context is for data many components need โ€” theme, auth, locale, language
โšก Wrap useContext in a custom hook (useTheme) for type safety and better errors
๐Ÿ“Œ Every consumer re-renders when the Provider value changes โ€” split contexts to limit re-renders
๐ŸŸข React 19+ lets you use <Context value={...}> directly without .Provider
contextproviderusecontext
Back to the full React Components & JSX cheat sheet