Skip to content

Instantly share code, notes, and snippets.

@timurcatakli
Created March 9, 2023 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timurcatakli/72e4a48f3f4609dbbc8c5c3e38b03d64 to your computer and use it in GitHub Desktop.
Save timurcatakli/72e4a48f3f4609dbbc8c5c3e38b03d64 to your computer and use it in GitHub Desktop.
import { useState } from "react";
export const useFetch = () => {
const [loading, setLoading] = useState(false);
const [error, setError] = useState("");
const [data, setData] = useState<any>([]);
const fetchData = async (url: string) => {
setLoading(true);
try {
const response = await fetch(url);
const data = await response.json();
setData(data);
return data;
} catch (error: any) {
setError(error.message);
} finally {
setLoading(false);
}
};
return { loading, error, data, fetchData };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment