以下是我用来设置状态的代码。
尽管数据库已经成功创建,但我无法调用
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.setState
,因为它始终未定义。
我尝试过: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
,但还是没有成功。
我该如何解决这个问题?