Rev 3498 | Rev 3658 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import { api } from '@api';
import { useApi } from './useApi';
import { useAlert } from './useAlert';
import { useEffect, useRef } from 'react';
const getResources = async (url, params) => {
return await api.get(url, { params });
};
export function useFetch(url, { params = undefined } = {}) {
const { showError } = useAlert();
const { loading, data, error, execute } = useApi(getResources);
const hasFetched = useRef(false);
useEffect(() => {
if (!hasFetched.current && url) {
execute(url, params);
hasFetched.current = true;
} else if (url || params) {
execute(url, params);
}
}, [url, params, execute]);
useEffect(() => {
if (error) showError(error);
}, [error, showError]);
return {
data,
loading,
refetch: () => execute(url, params)
};
}