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