Last change
on this file since bc20307 was bc20307, checked in by Tasevski2 <39170279+Tasevski2@…>, 3 years ago |
Push before video
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Rev | Line | |
---|
[bc20307] | 1 | import { useState, useEffect, useContext } from 'react';
|
---|
| 2 | import axios from 'axios';
|
---|
| 3 | import { AccessoriesContext } from '../context/AccessoriesContext';
|
---|
| 4 |
|
---|
| 5 | const 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 |
|
---|
| 38 | export default useGetData;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.