QuickDevGuide

Home › React

Context API — React

Context passes data through the component tree without threading props through every level ("prop drilling"). Typical uses: theme, current user, locale, feature flags. Three steps Create: const ThemeContext = createContext(defaultValue). Provide: wrap a subtree in <ThemeContext.Provider value={…}> (React 19 also allows <ThemeContext value={…}>). Consume: const theme = useContext(ThemeContext) anywhere below. Best practices Wrap context in a custom hook (useTheme()) that throws a helpful error when used outside its provider. Every consumer re-renders when the value changes — memoise the value object with useMemo, and split unrelated data into separate contexts. Combine with useReducer for small global stores. For large, frequently-changing state consider Zustand, Redux Toolkit or TanStack…

Key Takeaways