Hint
Subscribe to context value — every consumer re-renders when context value changes
// 1. Create context
const ThemeContext = createContext('light');
// 2. Provide value
function App() {
const [theme, setTheme] = useState('light');
return (
);
}
// 3. Consume anywhere in the tree
function Button() {
const { theme, setTheme } = useContext(ThemeContext);
return ;
}
When to avoid:
// ❌ One fat context — Button re-renders when user changes even if only needs theme
const AppContext = createContext({ theme, user, cart });
// ✅ Split contexts — each consumer only subscribes to what it needs
const ThemeContext = createContext(theme);
const UserContext = createContext(user);