Sometimes, we want to set body styles within our React app.
In this article, we’ll look at how to set body styles within our React app.
Set Body Styles with React
Within our React component, we can set the body style with plain JavaScript.
For instance, we can write:
componentDidMount(){
document.body.style.backgroundColor = "green";
}
componentWillUnmount(){
document.body.style.backgroundColor = null;
}
We set the document.body.style.backgroundColor
property to set the background color.
componentDidMount
lets us run code when the component mounts.
componentWillUnmount
runs when the component unmounts.
Conclusion
Within our React component, we can set the body style with plain JavaScript.