source: sources/client/src/hooks/useGetData.js

Last change on this file was bc20307, checked in by Tasevski2 <39170279+Tasevski2@…>, 2 years ago

Push before video

  • Property mode set to 100644
File size: 1.1 KB
Line 
1import { useState, useEffect, useContext } from 'react';
2import axios from 'axios';
3import { AccessoriesContext } from '../context/AccessoriesContext';
4
5const useGetData = ({ url }) => {
6 const [data, setData] = useState(null);
7 const [isLoading, setIsLoading] = useState(true);
8 const { setAlert } = useContext(AccessoriesContext);
9 const fetchData = async () => {
10 await axios
11 .get(url)
12 .then((res) => {
13 setData(res.data);
14 console.log(res.data);
15 })
16 .catch((err) => {
17 // ALERT FOR ERROR WITH ERROR MSG
18 setAlert({
19 type: 'error',
20 msg: 'Проблеми со серверот!', // TODO change msg to err.message
21 });
22 })
23 .finally(() => {
24 setIsLoading(false);
25 });
26 };
27 useEffect(() => {
28 fetchData();
29 }, []);
30 return {
31 data,
32 setData,
33 isLoading,
34 fetchData
35 };
36};
37
38export default useGetData;
Note: See TracBrowser for help on using the repository browser.