[英]How would I add a if..else statement in this code?
I am currently creating a quiz using ReactJS but I want to put in a if/else statement when the quiz has been complete, if the person playing the quiz gets a score below 3 I want it to have a sad face, if above 3 then a happy one, how would I put this into my code?我目前正在使用 ReactJS 创建一个测验,但我想在测验完成后放入 if/else 语句,如果参加测验的人得分低于 3 我希望它有一张悲伤的脸,如果高于 3 那么一个快乐的人,我如何将其放入我的代码中?
I have tried so far to incorporate the if statement but not having much luck到目前为止,我已经尝试合并 if 语句,但运气不佳
Result.js结果.js
import React from 'react';
const Result = ({ score, playAgain }) => (
<div className="score-board">
<div className="score">You scored {score} / 5 correct answers!</div>
<button className="playBtn" onClick={playAgain}>
Play again!
</button>
</div>
);
export default Result;
index.js索引.js
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import './assets/style.css';
import quizService from './quizService';
import QuestionBox from './components/QuestionBox';
import Result from './components/Result';
class QuizBee extends Component {
state = {
questionBank: [],
score: 0,
responses: 0,
};
getQuestions = () => {
quizService().then((question) => {
this.setState({
questionBank: question,
});
});
};
computeAnswer = (answer, correctAnswer) => {
if (answer == correctAnswer) {
this.setState({
score: this.state.score + 1,
});
}
this.setState({
responses: this.state.responses < 5 ? this.state.responses + 1 : 5,
});
};
playAgain = () => {
this.getQuestions();
this.setState({
score: 0,
responses: 0,
});
};
componentDidMount() {
this.getQuestions();
}
render() {
return (
<div className="container">
<div className="title">QuizBee</div>
{this.state.questionBank.length > 0 &&
this.state.responses < 5 &&
this.state.questionBank.map(({ question, answers, correct, questionId }) => (
<QuestionBox
question={question}
options={answers}
key={questionId}
selected={(answer) => this.computeAnswer(answer, correct)}
/>
))}
{this.state.responses === 5 ? <Result score={this.state.score} playAgain={this.playAgain} /> : null}
</div>
);
}
}
ReactDOM.render(<QuizBee />, document.getElementById('root'));
Inside the Result.js, you can add the logic as在 Result.js 中,您可以将逻辑添加为
import React from "react";
const Result = ({score, playAgain}) => (
<div className="score-board">
<div className="score">You scored {score} / 5 correct answers!</div>
{score <=3 ? <SadFace/> : <HappyFace/>}
<button className="playBtn" onClick={playAgain}>
Play again!
</button>
</div>
);
export default Result;
You can do it in your Result component:您可以在 Result 组件中执行此操作:
import React from "react";
const Result = ({score, playAgain}) => (
<div className="score-board">
<img src={score <= 3 ? "sad.png" : "happy.png" } /> // <----- HERE
<div className="score">You scored {score} / 5 correct answers!</div>
<button className="playBtn" onClick={playAgain}>
Play again!
</button>
</div>
);
export default Result;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.