如何在加载数据以获取数据时显示加载旋转器。
const [user, loading, error] = useAuthState(auth);
const navigate = useNavigate()
const [bikes] = useBikes()
if (loading) {
return <Loading></Loading>
}
fetch(`http://localhost:5000/bike/${id}`, {
method: 'DELETE'
})
.then(res => res.json())
.then(data => {
console.log(data)
alert('One Item Deleted')
});发布于 2022-05-12 15:28:51
如果在id spinner下面有旋转器,那么这里有一个关于如何做到这一点的极简示例。只需将fetch包装在函数中即可。
function delitem(){
document.getElementByID("spinner").style.display = "block"
fetch(`http://localhost:5000/bike/${id}`, {
method: 'DELETE'
})
.then(res => res.json())
.then(data => {
console.log(data)
alert('One Item Deleted')
document.getElementByID("spinner").style.display = "none"
});
}
发布于 2022-06-10 07:24:08
将fetch API放入useEffect中,这样您就可以轻松地实现spinner功能。
const [isLoading,setIsLoading]=useState(false);
useEffect(()=>{
setIsLoading(true);
fetch("some-url").then(res=>res.json())
.then(data=>{
setIsLoading(false)
console.log(data);
},[])
if(isLoading){
return <LoadingSpinner/>
}
return <div>..your data </div>https://stackoverflow.com/questions/72175376
复制相似问题