'Wait for user click then continue execution in React
I am making React Card game with hooks and all that. But I can't figure out how to implement wait for user click when certain condition is met (when is turn on human player for example). I need it to work on certaint buttons (cards), not global window click. I tried with async await and event listeners but can't get it to work (event listener not reconized, not sure if it works in React). If someone could help me out with template on how to do that. Thank you for the help in advance. Code sample is below
export function playTurn(...players) {
let cardsThrown = 0;
// for example if lastWinner or secondPLayer===human wait for on click on "classname"
whoPlaysFirst(...players);
let lastWinner = lastTurnWinner(...players);
const { hand: lastTurnWinnerHand } = lastWinner;
lastWinner.currentCard = lastWinner
.playCard(sample(lastTurnWinnerHand))
.shift();
Game.winningSuit = lastWinner.currentCard.suit;
cardsThrown += 1;
console.log(lastWinner.currentCard);
while (cardsThrown !== 4) {
let secondPlayer = nextPlayer(Game.lastPlayer);
secondPlayer.currentCard = secondPlayer
.playCard(sample(secondPlayer.hand))
.shift();
cardsThrown += 1;
console.log(secondPlayer.currentCard);
lastWinner = winningCard(lastWinner, secondPlayer); //Pobjednik ostaje
Game.lastPlayer = secondPlayer;
}
console.log("Pobjednicka karta je: ", lastWinner.currentCard);
nextTurn(lastWinner, ...players);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
