以下是我用来设置状态的代码。
handleAddNewQuiz(event){
this.quiz = new Quiz(this.db, this.newQuizName, function(err, affected, value){
if(!err){
this.setState( { quiz : value}); // ERROR: Cannot read property 'setState' of undefined
}
});
event.preventDefault();
};
尽管数据库已成功创建,但我无法调用this.state,因为它始终未定义。
我试过:
self = this;
handleAddNewQuiz(event){
this.quiz = new Quiz(this.db, this.newQuizName, function(err, affected, value){
if(!err){
self.setState( { quiz : value}); // ERROR: self.setState is not a function
}
});
event.preventDefault();
};
但它仍然失败,尝试使用a = this和 使用a.setState,仍然没有运气。
我该如何解决这个问题?