Я пытаюсь создать новое случайное число, которое будет установлено как состояние после метода. Он отлично работает при обновлении, но я не могу «сгенерировать» новый номер после нажатия кнопки.React.js сгенерирует новое случайное число
Вот мой код в данный момент:
var QuizContainer = React.createClass({
randomNumber: function(){
return Math.floor((Math.random() * Data.length) + 0);
},
getInitialState: function(){
var rand = this.randomNumber();
return {
answerList: Data[rand].answer,
answerQuestion: Data[rand].question,
correctAnswer: Data[rand].correct,
score: 0,
timer: 30
}
},
success: function(){
this.setState({score: this.state.score + 1})
},
fail: function(){
this.setState({score: 0})
this.getInitialState();
},
handleClick: function(child){
child.props.singleAnswer == this.state.correctAnswer ? this.success() : this.fail()
},
render: function(){
return(
<div>
<Timer timer={this.state.timer} />
<Score currentScore={this.state.score} />
<QuestionContainer answerQuestion={this.state.answerQuestion} />
<ButtonContainer onClick={this.handleClick} correctAnswer={this.state.correctAnswer} answerList={this.state.answerList} />
</div>
)
}
})
module.exports = QuizContainer;
Если кто может помочь я был бы очень благодарен! Благодаря!