import { useState } from 'react'; export function useApi( apiFunction: () => Promise ) { const [data, setData] = useState(null); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(''); const onApi = () => { setIsLoading(true); return apiFunction() .then((response) => { setData(response); return response; }) .catch((error) => { setError(error); return Promise.reject(error); }) .finally(() => { setIsLoading(false); }); }; return { data, error, isLoading, onApi }; }