以下是我用來設置狀態的代碼。如何做setState裏面的回調:ReactJS
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();
};
Rven儘管數據庫創建成功,我不能叫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
,仍然沒有運氣。
我該如何解決這個問題?
use()=> {}改爲使用function(){}。 – Ved