-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
53 lines (47 loc) · 1.42 KB
/
index.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
import {useCallback, useEffect, useRef, useState} from 'react'
export type State<T> = {
data: T
error?: undefined
} | {
data?: undefined
error: unknown
} | {
data?: undefined
error?: undefined
}
/**
* Only fetch on first render, to re-fetch call reload()
* @return {data, error, reload}
* data is undefined and error is undefined: the fetch is not finished
* data and error never be defined at the same time
* reload(): returns the result of fetchFn()
*/
export default function useFetch<T>(
fetchFn: () => Promise<T> | T, // never return undefined
getInitial?: () => T | undefined // may throw an error
): State<T> & {reload(this: void): Promise<T>} {
const [state, setState] = useState<State<T>>(() => {
if (!getInitial) return {}
try {
return {data: getInitial() as T}
} catch (error) {
return {error}
}
})
async function load(){
setState({})
const promise = (async () => fetchFn())() // Promise.try proposal
try {setState({data: await promise}) // https://github.com/reactwg/react-18/discussions/82
} catch (error) {setState({error})}
return promise
}
const loadRef = useRef(load)
loadRef.current = load
const initStateRef = useRef(state)
useEffect(() => {
// only load if data is not available
if (initStateRef.current.data === undefined && initStateRef.current.error === undefined) loadRef.current()
}, [])
const reload = useCallback(() => loadRef.current(), [])
return {...state, reload}
}