generated from Luis/nextjs-python-web-template
some final fixes before the demo
This commit is contained in:
32
pkgs/ui/src/components/hooks/useFetch.tsx
Normal file
32
pkgs/ui/src/components/hooks/useFetch.tsx
Normal file
@@ -0,0 +1,32 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user