Files
service-aware-frontend/pkgs/ui/src/components/hooks/useFetch.tsx
sara-pervana 0cf025d529
Some checks failed
checks-impure / test (pull_request) Successful in 2m4s
checks / test (pull_request) Failing after 8m51s
some final fixes before the demo
2023-12-12 14:02:43 +01:00

33 lines
672 B
TypeScript

import { useState, useEffect } from 'react';
import axios from 'axios';
import { BASE_URL } from '@/constants';
const useFetch = (url: string) => {
const [data, setData] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const fetch = () => {
setLoading(true);
axios.get(BASE_URL + url)
.then((response) => {
setData(response.data);
})
.catch((error) => {
setError(error);
})
.finally(() => {
setLoading(false);
});
};
useEffect(() => {
fetch();
}, [url]);
return { data, loading, error, fetch };
};
export default useFetch;