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上失败。
想法?
是组件正确地重新描绘或是否有错误发生的历史而重新呈现?因为回调只是在回归后才执行 –