I am doing a random quote app and it seems like I am using react state wrong when I try to change the body background and text-box text color. The body background color is one step behind all the time and I want them to change to the same color simultaneously. Any idea what is wrong with my code? I just added here the class component that is doing the business.
P.S. I hope my code is not too hard to read, I am a beginner
class Quote extends React.Component {
constructor() {
super();
this.state = {
quote: [],
author: [],
apiData:[],
color: ""
};
this.handleClick = this.handleClick.bind(this)
}
componentDidMount() {
//Here I made an API call for this.state.apiData to get my quotes
}
handleClick(){
var randomColor = '#'+Math.floor(Math.random()*16777215).toString(16); //Here I get a random color
function getRandNum(min, max){
return Math.floor(Math.random()*(max-min)+min) //Here I get a random number in the range of my API array length
}
let randomNum = getRandNum(0,this.state.apiData.length)
this.setState({
quote: this.state.apiData[randomNum].text,
author: this.state.apiData[randomNum].author
color: randomColor
})
document.body.style.backgroundColor = this.state.color;
console.log(this.state.color)
}
render() {
return (
<div class="quote-box">
<h1 style={{color: this.state.color}}> {this.state.quote}-"{this.state.author}" </h1>
<button
onClick={this.handleClick}
class="change-button"
style={{color: "white", backgroundColor: this.state.color}}
>Change
</button>
</div>
);
}
}