So I had to make a fetch call every time I load a page and the page will remain on a loading state until the data is fetched. So I made a function on a separate file the function takes 3 argument (link, setState for Data, setState for loading) and the function looked like this:
async function fetchingObj(link, dataFunc, loadingFunc){
const headers = {
'Content-Type': 'application/json',
'Authorization': `JWT ${localStorage.getItem('access')}`
}
try{
let data = await fetch(link, { headers }).then(data => data.json())
if(typeof data === "object" || typeof data === "string"){
dataFunc(data)
loadingFunc(false)
}
else{
setTimeout( fetchingObj(), 1500 )
console.log('from else', data);
}
}
catch(err){
setTimeout( fetchingObj(), 1500 )
}
}
and I imported it to my page and called it inside my useEffect
useEffect(() =>{
fetchingObj(process.env.REACT_APP_API_URL + `/api/polls/${parameters.num}/`, setPoll, setLOading)
//eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
So the purpose of this function is that if my server went down the page is going to stay on a loading state and keep refetching every 1,5 sec until my server is back up and running. However when I tested it by turning off my server I got like 100 error per second like this
and when my server is back on I still need a refresh, why is this happening?
thank you so much