import React, { createContext, useContext, useState } from "react";
const themes = {
light: {
foreground: "#000000",
background: "#eeeeee"
},
dark: {
foreground: "#ffffff",
background: "#222222"
}
};
const ThemeContext = createContext(themes.light);
function App() {
const [color, setColor] = useState(themes.dark);
const onColor = () => {
setColor(themes.light);
};
return (
<ThemeContext.Provider value={{ color, onColor }}>
<Toolbar />
</ThemeContext.Provider>
);
}
function Toolbar(props) {
return (
<div>
<ThemedButton />
</div>
);
}
function ThemedButton() {
const { color, onColor } = useContext(ThemeContext);
return (
<button
onClick={onColor}
style={{ background: color.background, color: color.foreground }}
>
I am styled by theme context!
</button>
);
}
export default App;