-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.cjs
49 lines (45 loc) · 1.38 KB
/
index.cjs
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
'use strict'
Object.defineProperty(exports, '__esModule', {value: true})
const react_1 = require('react')
/**
* 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()
*/
function useFetch(fetchFn, // never return undefined
getInitial // may throw an error
) {
const [state, setState] = (0, react_1.useState)(() => {
if (!getInitial)
return {}
try {
return {data: getInitial()}
} 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 = (0, react_1.useRef)(load)
loadRef.current = load
const initStateRef = (0, react_1.useRef)(state);
(0, react_1.useEffect)(() => {
// only load if data is not available
if (initStateRef.current.data === undefined && initStateRef.current.error === undefined)
loadRef.current()
}, [])
const reload = (0, react_1.useCallback)(() => loadRef.current(), [])
return Object.assign(Object.assign({}, state), {reload})
}
module.exports = useFetch
exports.default = useFetch