import { useEffect, useState } from 'react'; export const useFetch = ({ fetchData }) => { const [response, setResponse] = useState(); const [error, setError] = useState(); const [isLoading, setIsLoading] = useState(false); function fetch() { setError(false); setResponse(undefined); setIsLoading(true); fetchData() .then(res => { setResponse(res); }) .catch(err => { setError(err); }) .finally(() => { setIsLoading(false); }); } useEffect(() => { fetch(); }, []); return { response, isLoading, error, fetch }; };