import { useState, useEffect, useCallback } from "react"; export function useApi(fetcher: () => Promise) { const [data, setData] = useState(null); const [error, setError] = useState(null); const [loading, setLoading] = useState(true); const load = useCallback(() => { setLoading(true); fetcher() .then(setData) .catch(setError) .finally(() => setLoading(false)); }, [fetcher]); useEffect(() => { load(); }, [load]); return { data, error, loading, reload: load }; }