I am trying to change the background color of html body to red with a button click, but i only know how to do it with an element that is already inside body, not the body itself.
class App extends Component {
constructor(props) {
super(props);
this.state = {
bgColor: ""
};
}
boxClick = e => {
this.setState({
bgColor: "red"
});
};
render() {
return (
<div className="App">
<article className="experimentsHolder">
<div
className="boxClickCss"
style={{ backgroundColor: this.state.bgColor }}
onClick={this.boxClick}
>
Click Me!
</div>
</article>
</div>
);
}
}
As you can see, i added style={{backgroundColor: this.state.bgColor}} to div, but i can't add it inside body since it's not in this file. Any help?
document.body.style.backgroundColor = 'red';document.querySelector('body')document.getElementById('#body_id'), etc.backgroundColor, it's a poor choice because you're mixing presentation with logic. That is when the color changes, and another programmer wants to change it, there is not easy answer. Instead you should create a css class that does what you need and add/remove that class from the body html element.