So I'm writing a simple React.js app and just have a question about setting state, can this be done any cleaner?
const enemy = this.state.enemy;
if (this.state.isRock) {
enemy === "rock"
? this.setState({ result: "Draw!" })
: enemy === "paper"
? this.setState({ result: "You lose!" })
: enemy === "scissors"
? this.setState({ result: "You win!" })
: this.setState({ result: null });
} else if (this.state.isPaper) {
enemy === "rock"
? this.setState({ result: "You win!" })
: enemy === "paper"
? this.setState({ result: "Draw!" })
: enemy === "scissors"
? this.setState({ result: "You lose!" })
: this.setState({ result: null });
} else if (this.state.isScissors) {
enemy === "rock"
? this.setState({ result: "You lose!" })
: enemy === "paper"
? this.setState({ result: "You win!" })
: enemy === "scissors"
? this.setState({ result: "Draw!" })
: this.setState({ result: null });
}
Aucun commentaire:
Enregistrer un commentaire