使用Fetch API时在通话过程中显示加载旋转器
如何创建加载语句在获取时
useEffect(() => {
fetch('https://hidden-shore-3231.herokuapp.com/allCarsHomePage')
.then(res => res.json())
.then(data => {
setCards(data)
})
}, [])
how to create loading statement while it's fetching
useEffect(() => {
fetch('https://hidden-shore-3231.herokuapp.com/allCarsHomePage')
.then(res => res.json())
.then(data => {
setCards(data)
})
}, [])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试在获取之前直接创建加载旋转器,然后在解决承诺后隐藏 /删除它。
You could try creating the loading spinner right before fetching, and then hiding / removing it after the promise is resolved.
添加一个新状态,该状态是布尔值。当端点称为
true
时,端点被称为“更新该状态”,然后将数据发送并解析时,将状态设置回false
。同时,添加一个单独的
返回
,该在加载状态为true时显示旋转器。Add a new state that is a boolean. When the endpoint is called update that state with
true
, and then when the data has been sent, and parsed, set the state back tofalse
.Meanwhile add a separate
return
that shows the spinner if the loading state is true.