|
1 |
| -import { useState, useEffect, useMemo, useRef } from "react" |
| 1 | +import { useState, useEffect, useMemo, useCallback, useRef } from "react" |
2 | 2 |
|
3 | 3 | const useAsync = (arg1, arg2) => {
|
4 | 4 | const counter = useRef(0)
|
@@ -111,9 +111,14 @@ const useAsyncFetch = (input, init, options) => {
|
111 | 111 | const headers = input.headers || (init && init.headers) || {}
|
112 | 112 | const accept = headers["Accept"] || headers["accept"] || (headers.get && headers.get("accept"))
|
113 | 113 | 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 | + }) |
117 | 122 | }
|
118 | 123 |
|
119 | 124 | const unsupported = () => {
|
|
0 commit comments