Skip to content

Commit 70e12dd

Browse files
committed
Avoid dynamically calling a hook.
1 parent fc0cf6a commit 70e12dd

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/useAsync.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect, useMemo, useRef } from "react"
1+
import { useState, useEffect, useMemo, useCallback, useRef } from "react"
22

33
const useAsync = (arg1, arg2) => {
44
const counter = useRef(0)
@@ -111,9 +111,14 @@ const useAsyncFetch = (input, init, options) => {
111111
const headers = input.headers || (init && init.headers) || {}
112112
const accept = headers["Accept"] || headers["accept"] || (headers.get && headers.get("accept"))
113113
const doFetch = (input, init) => window.fetch(input, init).then(parseResponse(accept))
114-
return ~["POST", "PUT", "PATCH", "DELETE"].indexOf(method)
115-
? useAsync({ ...options, deferFn: (_, __, { signal }) => doFetch(input, { signal, ...init }) })
116-
: useAsync({ ...options, promiseFn: (_, { signal }) => doFetch(input, { signal, ...init }) })
114+
const fn = ~["POST", "PUT", "PATCH", "DELETE"].indexOf(method) ? "deferFn" : "promiseFn"
115+
return useAsync({
116+
...options,
117+
[fn]: useCallback(
118+
(_, props, ctrl) => doFetch(input, { signal: ctrl ? ctrl.signal : props.signal, ...init }),
119+
[JSON.stringify(input), JSON.stringify(init)]
120+
),
121+
})
117122
}
118123

119124
const unsupported = () => {

0 commit comments

Comments
 (0)