import { useCallback, useEffect, useState } from "react"; interface UseFetch { fetchFn: () => Promise; initData: T; } export const useFetch = ({ fetchFn, initData }: UseFetch) => { const [data, setData] = useState(initData); const [isLoaded, setIsLoaded] = useState(false); const [error, setError] = useState(null); const fetchData = useCallback(async () => { try { const response = await fetchFn(); setData(response); } catch (error) { setError(error as Error); } finally { setIsLoaded(true); } }, [fetchFn]); useEffect(() => { fetchData(); }, []); return { data, error, isLoaded }; };