📅  最后修改于: 2022-03-11 14:56:00.685000             🧑  作者: Mango
import { useState, useEffect } from "react";
const useFetch = (url) => {
const [data, setData] = useState(null);
const [isPending, setIsPending] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
fetch(url)
.then((res) => {
console.log(res);
if (!res.ok) {
throw Error("could not fetch data for that resource");
}
return res.json();
})
.then((data) => {
setData(data);
setIsPending(false);
setError(null);
})
.catch((err) => {
setIsPending(false);
setError(err.message);
});
}, []);
return { data, isPending, error };
};
export default useFetch;