@@ -29,7 +29,7 @@ const WITH_DEDUPE = { dedupe: true }
29
29
30
30
export const useSWRHandler = < Data = any , Error = any > (
31
31
_key : Key ,
32
- fn : Fetcher < Data > | null ,
32
+ fetcher : Fetcher < Data > | null ,
33
33
config : typeof defaultConfig & SWRConfiguration < Data , Error >
34
34
) => {
35
35
const {
@@ -68,6 +68,7 @@ export const useSWRHandler = <Data = any, Error = any>(
68
68
69
69
// Refs to keep the key and config.
70
70
const keyRef = useRef ( key )
71
+ const fetcherRef = useRef ( fetcher )
71
72
const configRef = useRef ( config )
72
73
const getConfig = ( ) => configRef . current
73
74
@@ -96,7 +97,7 @@ export const useSWRHandler = <Data = any, Error = any>(
96
97
97
98
// Resolve the current validating state.
98
99
const resolveValidating = ( ) => {
99
- if ( ! key || ! fn ) return false
100
+ if ( ! key || ! fetcher ) return false
100
101
if ( cache . get ( keyValidating ) ) return true
101
102
102
103
// If it's not mounted yet and it should revalidate on mount, revalidate.
@@ -117,7 +118,14 @@ export const useSWRHandler = <Data = any, Error = any>(
117
118
// `fetcher`, to correctly handle the many edge cases.
118
119
const revalidate = useCallback (
119
120
async ( revalidateOpts ?: RevalidatorOptions ) : Promise < boolean > => {
120
- if ( ! key || ! fn || unmountedRef . current || getConfig ( ) . isPaused ( ) ) {
121
+ const currentFetcher = fetcherRef . current
122
+
123
+ if (
124
+ ! key ||
125
+ ! currentFetcher ||
126
+ unmountedRef . current ||
127
+ getConfig ( ) . isPaused ( )
128
+ ) {
121
129
return false
122
130
}
123
131
@@ -182,7 +190,7 @@ export const useSWRHandler = <Data = any, Error = any>(
182
190
183
191
// Start the request and keep the timestamp.
184
192
CONCURRENT_PROMISES_TS [ key ] = getTimestamp ( )
185
- CONCURRENT_PROMISES [ key ] = fn ( ...fnArgs )
193
+ CONCURRENT_PROMISES [ key ] = currentFetcher ( ...fnArgs )
186
194
}
187
195
188
196
// Wait until the ongoing request is done. Deduplication is also
@@ -319,8 +327,9 @@ export const useSWRHandler = <Data = any, Error = any>(
319
327
[ ]
320
328
)
321
329
322
- // Always update config.
330
+ // Always update fetcher and config refs .
323
331
useIsomorphicLayoutEffect ( ( ) => {
332
+ fetcherRef . current = fetcher
324
333
configRef . current = config
325
334
} )
326
335
0 commit comments