Я создал приложение для викторины, используя React: https://codesandbox.io/s/eloquent-kare-gczfw
Было бы здорово получить отзывы, особенно анимацию, которая происходит при нажатии кнопки «Далее». Я понял, что анимация происходит вначале, потом не происходит, потом повторяется. Интересно, что мне не хватает, чтобы анимация происходила при каждом нажатии кнопки «Далее».
Я создал поле состояния «Анимация» в компоненте Викторина, чтобы оно переходило от ложного к истинному, и наоборот versa.
class Quiz extends React.Component {
state = {
currentQuiz: 0,
numOfQuestions: 0,
score: 0,
resultSummary: false,
animation: false
};
handleNextQuestion = answer => {
const { currentQuiz, numOfQuestions, score } = this.state;
if (answer) {
this.setState({
score: score + 1,
numOfQuestions: numOfQuestions + 1
});
} else {
this.setState({
numOfQuestions: numOfQuestions + 1
});
}
if (numOfQuestions === quizzes[currentQuiz].questions.length - 1) {
this.setState({
numOfQuestions,
resultSummary: true
});
}
if (!this.state.animation) {
this.setState({ animation: "multiple-choices" });
} else {
this.setState({ animation: false });
}
};
handleNextQuiz = () => {
const { currentQuiz } = this.state;
if (currentQuiz + 1 === quizzes.length) {
this.setState({
resultSummary: false,
numOfQuestions: 0,
score: 0,
currentQuiz: 0
});
} else {
this.setState({
resultSummary: false,
numOfQuestions: 0,
score: 0,
currentQuiz: currentQuiz + 1
});
}
};
render() {
const {
currentQuiz,
numOfQuestions,
score,
resultSummary,
animation
} = this.state;
const { title, questions } = quizzes[currentQuiz];
const currentQuestion = questions[numOfQuestions];
const shuffledAnswerChoices = shuffle([
...currentQuestion.incorrectAnswers,
currentQuestion.correctAnswer
]);
return (
<div className="container">
<h1>{title}</h1>
{resultSummary ? (
<Result
score={score}
numOfQuestions={numOfQuestions}
nextQuizHandler={this.handleNextQuiz}
/>
) : (
<Question
currentQuestion={currentQuestion}
shuffledAnswerChoices={shuffledAnswerChoices}
nextQuestionHandler={this.handleNextQuestion}
animation={animation}
/>
)}
</div>
);
}
}
export default Quiz;
Также интересно, есть ли 1) лучший способ управления состоянием в этом приложении и 2) советы по медиазапросам для мобильных устройств (я вроде новичка в адаптивном дизайне).