I am passing a variable name theme with the help of useContext to every component in the react. I want to change the bgColor of body or root element as my theme changes as per the following cases. Simply if
- theme === light : bgColor => white
- theme === dark : bgColor => #2d2d2d
- theme === intense-dark : bgColor => black
How can access the CSS properties of root/body element in any of the React Component or vice-versa ( how can I access the value of variable theme in the `App.css )? Any of both ways is fine for me.
I am changing the theme as follows in one of my React component?
import { useContext } from "react";
import Notes from "./Notes";
import themeContext from "../context/themes/themeContext";
const Home = (props) => {
const contextForThemes = useContext(themeContext);
const { theme } = contextForThemes;
let noteStyle = {
backgroundColor: "white",
color: "black",
};
if (theme.light) {
noteStyle = { backgroundColor: "white", color: "black" };
} else if (theme.dark) {
noteStyle = {
backgroundColor: "#2d2d2d",
color: "whitesmoke",
};
} else if (theme.anotherDark) {
noteStyle = {
backgroundColor: "black",
color: "#84ff00",
};
}
return (
<div style={noteStyle}>
<Notes showAlert={props.showAlert} />
</div>
);
};
export default Home;