import { useEffect, useState } from "react";
const useFetch = () => {
const [data, setData] = useState(null);
const [isPending, setIsPending] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
const abortCont = new AbortController()
setTimeout(() => {
fetch('http://localhost:3000/blogs', {signal: AbortController.signal})
.then(res => {
console.log(res)
if(!res.ok){
throw Error('Could no fetch the data for that resource')
}
return res.json();
})
.then(data => {
setData(data)
console.log(data)
setIsPending(false)
setError(null)
})
.catch(err => {
if(err.name === 'AbortError')
console.log('fetch aborted')
setIsPending(false)
setError(err.message)
}, 1000)
});
return() => abortCont.abort()
}, []);
return {data, isPending, error}
}
export default useFetch;
i was trying to view my blogs by id and i am receiving a blank page
2
Answers
Try with this