React 文档指出 ajax 请求应该从componentDidMount
生命周期事件发起 (参见react 文档)。
为什么要举办这个活动?
在大多数情况下,当使用ajax加载数据时,我想显示某种加载栏,例如:
componentDidMount() {
this.setState({isLoading: true});
fetch(...)
.then(...)
.then(() => this.setState({isLoading: false})
}
但这会触发render
方法 3 次(初始渲染立即执行,然后设置isLoading = true
,然后执行isLoading = false
从componentWillMount
事件发送ajax请求有什么问题?