当React JS中的Fetch API从服务器加载数据时,如何显示旋转器?
我正在开发一个React应用程序,必须在主页上从服务器上加载数据。从服务器加载数据时,需要少量时间。当调用fetch API时,我想显示旋转器。我已经创建了一个旋转器组件,但是当触发fetch API时,我不知道如何显示旋转器。
const [product,setProduct]=useState({});
useEffect(()=>{
fetch(`http://localhost:5000/readSingleCarsData/${id}`)
.then(res=>res.json())
.then(data=> {
setProduct(data)
setQuantity(data.quantity)
});
},[])
I am developing a React app where I have to load data from the server on the home page. It takes a small amount of time when loading data from the server. I want to display a spinner when the fetch api is called. I already created a spinner component, but I don't know how to display that spinner when the fetch api is triggered.
const [product,setProduct]=useState({});
useEffect(()=>{
fetch(`http://localhost:5000/readSingleCarsData/${id}`)
.then(res=>res.json())
.then(data=> {
setProduct(data)
setQuantity(data.quantity)
});
},[])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用钩子控制加载程序状态,然后使用
.finally()
将加载状态转换回false。Control your loader state with hooks and use
.finally()
to convert your loading state back to false.尝试一下
Try this