I'm working on a matching game and have written a handleClick function that should increment a state value called guessCount so I can keep track of each time a player guesses. I also want to save the IDs of each item clicked as guessOne and guessTwo in state. For some reason the first click won't trigger my setState guessCount increment code nor capture the clicked item's IDs. Each click after does work correctly, except that it's one click behind the user's actual clicks.
state = {
guessCount: 0,
guessOne: null,
guessTwo: null
}
handleClick = (e) => {
this.setState({ guessCount: this.state.guessCount + 1 });
if(this.state.guessCount === 1) {
this.setState({ guessOne: e.currentTarget.dataset.id });
this.setState({ guessCount: this.state.guessCount + 1 });
}
if(this.state.guessCount === 2) {
this.setState({ guessTwo: e.currentTarget.dataset.id });
this.setState({ guessCount: 0 });
}
}
Expected results: On first click, the guessCount would increment from 0 to 1 and the clicked item's ID would be set in state to guessOne. On second click the guessCount would increment from 1 to 2, the clicked item's ID would be set in state to guessTwo, then the guessCount would be reset to 0.