I'm creating a typing game, and I want to update the points depending on if the input is correct or not. on initial click, regardless of right or wrong, the points read as 0 (they should update). if the next click is correct or not, the points will read the value update from the previous click so even a success will register as a failure and -5 points. why is my state being read incorrectly/late?
import React, { Component } from 'react';
import './typingGame.scss';
class TypingGame extends Component {
constructor() {
super();
this.state = {
points: 0,
}
}
checkInput = () => {
var helloWorld = document.getElementById('helloWorld').innerHTML;
var input = document.getElementById('userInput').value;
if (input !== helloWorld) {
this.setState({ points: this.state.points - 5 });
console.log("failure");
} else {
this.setState({ points: this.state.points + 5 });
console.log("success")
}
console.log(this.state.points)
}
render() {
return (
<section>
<p id="helloWorld">hello world</p>
<input id="userInput"></input>
<button onClick={this.checkInput}></button>
</section>
);
}
}
export default TypingGame;