react-interactive-quiz-component
Version:
Simple Interactive Quiz component
49 lines (43 loc) • 1.34 kB
JavaScript
import React, { Component } from 'react';
import Quiz from './Quiz';
import Congratulations from './Congratulations';
class PlayQuiz extends Component {
constructor(props) {
super(props);
this.questions = this.props.quiz;
this.no=0;
this.Answered = [];
this.isEnd = false;
this.isContact = false;
this.NextQuestion = this.NextQuestion.bind(this);
}
NextQuestion(questionAnswered)
{
if(this.no < this.questions.length-1){
this.no++;
}
else
this.isEnd=true;
this.Answered.push(questionAnswered);
this.forceUpdate();
}
componentWillReceiveProps(newProps){
this.no=0;
this.questions = newProps.quiz;
this.isEnd=false;
this.Answered=[];
}
render() {
return (
<div>
{ this.isEnd == false &&
<Quiz question={this.questions[this.no]} onNext={this.NextQuestion}/>
}
{this.isEnd == true && this.isContact == false &&
<Congratulations reviewAnswers = {this.Answered} email={this.props.doContact.email} />
}
</div>
);
}
}
export default PlayQuiz;