【发布时间】:2025-11-24 09:05:02
【问题描述】:
React 文档指出 ajax 请求应该从 componentDidMount 生命周期事件发起(参见 react docs)。
为什么要举办这个活动?
在大多数情况下,使用 ajax 加载数据时,我想显示某种加载栏,例如:
componentDidMount() {
this.setState({isLoading: true});
fetch(...)
.then(...)
.then(() => this.setState({isLoading: false})
}
但这会触发render 方法3 次(初始渲染立即设置isLoading = true,然后设置isLoading = false
从componentWillMount事件发送ajax请求有什么问题?
【问题讨论】:
-
@mattias:谢谢,我发现链接问题中的答案非常有用,我建议任何对此问题感兴趣的人看看它。
标签: reactjs