@@ -166,26 +166,22 @@ const useAsyncFetch = (input, init, { defer, json, ...options } = {}) => {
166
166
const accept = headers [ "Accept" ] || headers [ "accept" ] || ( headers . get && headers . get ( "accept" ) )
167
167
const doFetch = ( input , init ) => globalScope . fetch ( input , init ) . then ( parseResponse ( accept , json ) )
168
168
const isDefer =
169
- typeof defer === "boolean" ? defer : ~ [ "POST" , "PUT" , "PATCH" , "DELETE" ] . indexOf ( method )
169
+ typeof defer === "boolean" ? defer : [ "POST" , "PUT" , "PATCH" , "DELETE" ] . indexOf ( method ) !== - 1
170
170
const fn = isDefer ? "deferFn" : "promiseFn"
171
- const identity = JSON . stringify ( { input, init, fn } )
171
+ const identity = JSON . stringify ( { input, init, isDefer } )
172
172
const state = useAsync ( {
173
173
...options ,
174
174
[ fn ] : useCallback (
175
- isDefer
176
- ? ( [ override ] , _ , { signal } ) =>
177
- doFetch ( input , {
178
- signal,
179
- ...// override is a function, call it with init
180
- ( typeof override === "function"
181
- ? override ( init )
182
- : // override is an Event or SyntheticEvent - do not spread
183
- "preventDefault" in override
184
- ? init
185
- : // otherwise, spread override over init
186
- { ...init , ...override } ) ,
187
- } )
188
- : ( _ , { signal } ) => doFetch ( input , { signal, ...init } ) ,
175
+ ( arg1 , arg2 , arg3 ) => {
176
+ const [ override , signal ] = arg3 ? [ arg1 [ 0 ] , arg3 . signal ] : [ undefined , arg2 . signal ]
177
+ if ( typeof override === "object" && "preventDefault" in override ) {
178
+ // Don't spread Events or SyntheticEvents
179
+ return doFetch ( input , { signal, ...init } )
180
+ }
181
+ return typeof override === "function"
182
+ ? doFetch ( input , { signal, ...override ( init ) } )
183
+ : doFetch ( input , { signal, ...init , ...override } )
184
+ } ,
189
185
[ identity ] // eslint-disable-line react-hooks/exhaustive-deps
190
186
) ,
191
187
} )
0 commit comments