@@ -35,7 +35,7 @@ const WITH_DEDUPE = { dedupe: true }
35
35
36
36
export const useSWRHandler = < Data = any , Error = any > (
37
37
_key : Key ,
38
- fn : Fetcher < Data > | null ,
38
+ fetcher : Fetcher < Data > | null ,
39
39
config : typeof defaultConfig & SWRConfiguration < Data , Error >
40
40
) => {
41
41
const {
@@ -74,6 +74,7 @@ export const useSWRHandler = <Data = any, Error = any>(
74
74
75
75
// Refs to keep the key and config.
76
76
const keyRef = useRef ( key )
77
+ const fetcherRef = useRef ( fetcher )
77
78
const configRef = useRef ( config )
78
79
const getConfig = ( ) => configRef . current
79
80
@@ -106,7 +107,7 @@ export const useSWRHandler = <Data = any, Error = any>(
106
107
107
108
// Resolve the current validating state.
108
109
const resolveValidating = ( ) => {
109
- if ( ! key || ! fn ) return false
110
+ if ( ! key || ! fetcher ) return false
110
111
if ( cache . get ( keyValidating ) ) return true
111
112
112
113
// If it's not mounted yet and it should revalidate on mount, revalidate.
@@ -127,7 +128,14 @@ export const useSWRHandler = <Data = any, Error = any>(
127
128
// `fetcher`, to correctly handle the many edge cases.
128
129
const revalidate = useCallback (
129
130
async ( revalidateOpts ?: RevalidatorOptions ) : Promise < boolean > => {
130
- if ( ! key || ! fn || unmountedRef . current || getConfig ( ) . isPaused ( ) ) {
131
+ const currentFetcher = fetcherRef . current
132
+
133
+ if (
134
+ ! key ||
135
+ ! currentFetcher ||
136
+ unmountedRef . current ||
137
+ getConfig ( ) . isPaused ( )
138
+ ) {
131
139
return false
132
140
}
133
141
@@ -196,7 +204,7 @@ export const useSWRHandler = <Data = any, Error = any>(
196
204
197
205
// Start the request and keep the timestamp.
198
206
CONCURRENT_PROMISES_TS [ key ] = getTimestamp ( )
199
- CONCURRENT_PROMISES [ key ] = fn ( ...fnArgs )
207
+ CONCURRENT_PROMISES [ key ] = currentFetcher ( ...fnArgs )
200
208
}
201
209
202
210
// Wait until the ongoing request is done. Deduplication is also
@@ -345,8 +353,9 @@ export const useSWRHandler = <Data = any, Error = any>(
345
353
[ ]
346
354
)
347
355
348
- // Always update config.
356
+ // Always update fetcher and config refs .
349
357
useIsomorphicLayoutEffect ( ( ) => {
358
+ fetcherRef . current = fetcher
350
359
configRef . current = config
351
360
} )
352
361
0 commit comments