我有一個Button Listener,通過superagent傳遞一些登錄信息,我得到一個令牌返回。現在我想更新我的狀態屬性到收到的令牌,我正確地得到。但是我的this.state.token似乎沒有更新。爲什麼我不能在superagent請求中更新我的狀態屬性?
Here's我的代碼:
_btnLoginListener: function() {
var that = this;
superAgentRequest
.post(URL + TOKENS_KEY)
.type('json')
.send({
email: this.state.usernameOrEmail,
password: this.state.password
})
.end(function (err, res) {
// Calling the end function will send the superAgentRequest
if (res.status == 200) {
/*var token = JSON.parse(res.text).token; // working*/
/*ToastAndroid.show("POST Response -> " + token, ToastAndroid.SHORT)*/
that.setState({
token: JSON.parse(res.text)
})
navigator.replace({
id: 'MainController',
name: 'MainController',
});
} else {
notifyMessage ("Please check credentials again!")
}
})
}
notifyMessage (that.state.token)
},
異步代碼異步 – zerkms