forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseFetch.js
More file actions
36 lines (32 loc) · 1.14 KB
/
Copy pathuseFetch.js
File metadata and controls
36 lines (32 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { useState, useEffect, useCallback, useRef } from 'react';
import api from '../lib/api';
/**
* Generic data-fetching hook backed by the project's axios instance.
*
* @param {string|null} url Relative API path. Pass null to skip fetching.
* @param {{ method?: string, body?: any }} [options]
* @returns {{ data: any, loading: boolean, error: string|null, refetch: () => void }}
*/
export function useFetch(url, options = {}) {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(!!url);
const [error, setError] = useState(null);
const optsRef = useRef(options);
optsRef.current = options;
const execute = useCallback(async () => {
if (!url) return;
setLoading(true);
setError(null);
try {
const { method = 'get', body } = optsRef.current;
const res = await api.request({ url, method, data: body });
setData(res.data);
} catch (err) {
setError(err?.response?.data?.message || err?.message || 'Request failed');
} finally {
setLoading(false);
}
}, [url]);
useEffect(() => { execute(); }, [execute]);
return { data, loading, error, refetch: execute };
}