1
考慮以下的簡單代碼:無法在componentDidMount的setState上調用回調函數? - 陣營
componentDidMount() {
this._fetchData();
}
_fetchData() {
let url = UrlFormatter() + '/api/v1/blogs/';
$.get(url, (result) => {
if (result.status === 401) {
this.setState({
error: 'Your session has expired. We cannot load data.',
});
} else {
console.log('obvs here');
this.setState({
error: null,
data: result,
},() => {
console.log('dasddsa');
this._setUpPostCollapseStatus();
});
}
}).fail((response) => {
this.setState({
error: 'Could not fetch blogs, something went wrong.'
});
});
}
如果我們調查,我們看到控制檯:
obvs here
但我們從來沒有看到:dasddsa
,現在要麼這是一個錯誤,或者你不能在componentDidMount
setState
上調用回調函數 - 或者我在ES6上失敗。
想法?
是組件正確地重新描繪或是否有錯誤發生的歷史而重新呈現?因爲回調只是在迴歸後才執行 –