Hey! I'm Emre and this is my Rock,Paper,Scissors project
There are some problems with my project
- Score doesn't seem to be updating correctly
- Score takes negative values
I think the main problem is due to the use of useEffect
Any suggestions on how I can improve are welcome
GameContext.js
const onSelect = () => {
const userSelect = GameRules[selection].value;
const number = Math.floor(Math.random() * 3);
setHouseSelection(number);
setTimeout(() => {
setShow(true);
if (GameRules[houseSelection].beats.includes(userSelect)) {
setResult("You Lose");
setScore((score) => score - 1);
} else {
if (GameRules[houseSelection].value == userSelect) {
setResult("Tie");
} else {
setResult("You Win");
setScore((score) => score + 1);
}
}
}, 3000);
};
containers\game\index.js
const { onSelect, houseSelection, result, show, playAgain } =
useContext(GameContext);
useEffect(() => {
onSelect();
}, [houseSelection]);