【发布时间】:2016-07-01 23:03:13
【问题描述】:
考虑以下简单代码:
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 上失败了。
想法?
【问题讨论】:
-
组件是否正确重新渲染或重新渲染时出现错误?因为回调只在重新渲染后执行
标签: javascript reactjs