Theme Switcher
Combine context and state for a theme toggle.
Example
const ThemeCtx = createContext({ theme: "light", toggle() {} });
function ThemeProvider({ children }) {
const [theme, setTheme] = useState("light");
const toggle = () => setTheme((t) => (t === "light" ? "dark" : "light"));
return <ThemeCtx.Provider value={{ theme, toggle }}>{children}</ThemeCtx.Provider>;
}Exercise
Provide a value.
<ThemeCtx. value={x}>
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.