-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathquery-hook.ts
62 lines (58 loc) · 1.77 KB
/
query-hook.ts
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { DependencyList, useMemo, useState } from 'react';
import { useDeepCompareEffect } from './deep-compare-hooks';
export type QueryStatus<T> = {
isLoading: boolean;
isSuccess: boolean;
isError: boolean;
data: T | undefined;
error: string | undefined;
};
export function useQuery<T>(
queryFunction: (signal: AbortSignal) => Promise<T>,
dependencies: DependencyList,
useEffectFunction = useDeepCompareEffect
): QueryStatus<T> {
const [isLoading, setIsLoading] = useState<boolean>(true);
const [isError, setIsError] = useState<boolean>(false);
const [isSuccess, setIsSuccess] = useState<boolean>(false);
const [data, setData] = useState<T | undefined>(undefined);
const [error, setError] = useState<string | undefined>(undefined);
useEffectFunction(() => {
let isSubscribed = true;
const controller = new AbortController();
const signal = controller.signal;
setIsLoading(true);
setIsError(false);
setIsSuccess(false);
setData(undefined);
setError(undefined);
queryFunction(signal)
.then(_data => {
if (!isSubscribed) {
return;
}
setIsLoading(false);
setData(_data);
setIsSuccess(true);
})
.catch(_error => {
// TODO: Think about it if it would be a good idea to log such errors??
console.error(_error);
if (!isSubscribed) {
return;
}
setIsLoading(false);
setError(_error.message);
setIsError(true);
});
return () => {
isSubscribed = false;
controller.abort();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, dependencies);
return useMemo(
() => ({ isLoading, isSuccess, isError, data, error }),
[isLoading, isSuccess, isError, data, error]
);
}