@@ -352,31 +352,34 @@ export function useFavicon(url) {
352
352
} , [ url ] ) ;
353
353
}
354
354
355
+ const useFetchInitialState = {
356
+ error : undefined ,
357
+ data : undefined ,
358
+ } ;
359
+
360
+ const useFetchReducer = ( state , action ) => {
361
+ switch ( action . type ) {
362
+ case "loading" :
363
+ return { ...useFetchInitialState } ;
364
+ case "fetched" :
365
+ return { ...useFetchInitialState , data : action . payload } ;
366
+ case "error" :
367
+ return { ...useFetchInitialState , error : action . payload } ;
368
+ default :
369
+ return state ;
370
+ }
371
+ } ;
372
+
355
373
export function useFetch ( url , options ) {
356
374
const cacheRef = React . useRef ( { } ) ;
357
375
const ignoreRef = React . useRef ( false ) ;
358
376
359
- const initialState = {
360
- error : undefined ,
361
- data : undefined ,
362
- } ;
363
-
364
- const reducer = ( state , action ) => {
365
- switch ( action . type ) {
366
- case "loading" :
367
- return { ...initialState } ;
368
- case "fetched" :
369
- return { ...initialState , data : action . payload } ;
370
- case "error" :
371
- return { ...initialState , error : action . payload } ;
372
- default :
373
- return state ;
374
- }
375
- } ;
376
-
377
- const [ state , dispatch ] = React . useReducer ( reducer , initialState ) ;
377
+ const [ state , dispatch ] = React . useReducer (
378
+ useFetchReducer ,
379
+ useFetchInitialState
380
+ ) ;
378
381
379
- const onFetch = React . useEffectEvent ( ( ) => {
382
+ const onFetch = React . useEffectEvent ( ( url ) => {
380
383
return fetch ( url , options ) ;
381
384
} ) ;
382
385
@@ -393,7 +396,7 @@ export function useFetch(url, options) {
393
396
}
394
397
395
398
try {
396
- const res = await onFetch ( ) ;
399
+ const res = await onFetch ( url ) ;
397
400
398
401
if ( ! res . ok ) {
399
402
throw new Error ( res . statusText ) ;
0 commit comments