@@ -34,6 +34,7 @@ export const useInfiniteDataLoader = <
34
34
getOrAddRequest,
35
35
computeKey,
36
36
onError : onGlobalError ,
37
+ defaultDatalifetime,
37
38
} = useDataLoaderContext ( )
38
39
const {
39
40
enabled = true ,
@@ -44,24 +45,32 @@ export const useInfiniteDataLoader = <
44
45
dataLifetime,
45
46
getNextPage,
46
47
} = config ?? { }
48
+ const computedDatalifetime = dataLifetime ?? defaultDatalifetime
47
49
const requestRefs = useRef < DataLoader < ResultType , ErrorType > [ ] > ( [ ] )
48
50
const [ page , setPage ] = useState ( baseParams [ pageParamKey ] )
49
51
const nextPageRef = useRef ( page )
50
- const getNextPageRef = useRef ( getNextPage )
51
- const methodRef = useRef ( ( ) =>
52
- method (
53
- pageParamKey && page
54
- ? ( { ...baseParams , [ pageParamKey ] : page } as ParamsType )
55
- : baseParams ,
56
- ) ,
52
+
53
+ const getNextPageFnRef = useRef (
54
+ ( ...params : Parameters < NonNullable < typeof getNextPage > > ) =>
55
+ getNextPage ? getNextPage ( ...params ) : undefined ,
56
+ )
57
+
58
+ const getParamsRef = useRef ( ( ) => ( {
59
+ ...baseParams ,
60
+ [ pageParamKey ] : nextPageRef . current ,
61
+ } ) )
62
+
63
+ const getMethodRef = useRef ( ( ) => method ( getParamsRef . current ( ) ) )
64
+
65
+ const getOnSuccessRef = useRef (
66
+ ( ...params : Parameters < NonNullable < typeof onSuccess > > ) =>
67
+ onSuccess ?.( ...params ) ,
57
68
)
58
- const paramsRef = useRef (
59
- pageParamKey && page
60
- ? ( { ...baseParams , [ pageParamKey ] : page } as ParamsType )
61
- : baseParams ,
69
+
70
+ const getOnErrorRef = useRef (
71
+ ( err : ErrorType ) => onError ?.( err ) ?? onGlobalError ?.( err ) ,
62
72
)
63
- const onSuccessRef = useRef ( onSuccess )
64
- const onErrorRef = useRef ( onError ?? onGlobalError )
73
+
65
74
const [ , setCounter ] = useState ( 0 )
66
75
67
76
const forceRerender = useCallback ( ( ) => {
@@ -99,7 +108,7 @@ export const useInfiniteDataLoader = <
99
108
if ( ! requestInRef ) {
100
109
const request = getOrAddRequest < ResultType , ErrorType > ( currentQueryKey , {
101
110
enabled,
102
- method : methodRef . current ,
111
+ method : getMethodRef . current ,
103
112
} )
104
113
requestRefs . current . push ( request )
105
114
request . addObserver ( forceRerender )
@@ -117,12 +126,12 @@ export const useInfiniteDataLoader = <
117
126
! ! (
118
127
enabled &&
119
128
( ! request . dataUpdatedAt ||
120
- ! dataLifetime ||
129
+ ! computedDatalifetime ||
121
130
( request . dataUpdatedAt &&
122
- dataLifetime &&
123
- request . dataUpdatedAt + dataLifetime < Date . now ( ) ) )
131
+ computedDatalifetime &&
132
+ request . dataUpdatedAt + computedDatalifetime < Date . now ( ) ) )
124
133
) ,
125
- [ enabled , request . dataUpdatedAt , dataLifetime ] ,
134
+ [ enabled , request . dataUpdatedAt , computedDatalifetime ] ,
126
135
)
127
136
128
137
const optimisticIsLoadingRef = useRef ( needLoad )
@@ -149,64 +158,79 @@ export const useInfiniteDataLoader = <
149
158
const reload = useCallback ( async ( ) => {
150
159
await Promise . all (
151
160
requestRefs . current . map ( req =>
152
- req . load ( true ) . then ( onSuccessRef . current ) . catch ( onErrorRef . current ) ,
161
+ req
162
+ . load ( true )
163
+ . then ( getOnSuccessRef . current )
164
+ . catch ( getOnErrorRef . current ) ,
153
165
) ,
154
166
)
155
167
} , [ ] )
156
168
157
- useEffect ( ( ) => {
158
- request . method = ( ) => method ( paramsRef . current )
159
- } , [ method , request ] )
160
-
161
- useEffect ( ( ) => {
162
- onSuccessRef . current = onSuccess
163
- } , [ onSuccess ] )
169
+ const loadMore = useCallback ( ( ) => {
170
+ const nextPage = nextPageRef . current
171
+ if ( nextPage ) {
172
+ setPage ( ( ) => nextPage )
173
+ getParamsRef . current = ( ) => ( {
174
+ ...baseParams ,
175
+ [ pageParamKey ] : nextPage ,
176
+ } )
177
+ }
178
+ } , [ baseParams , pageParamKey ] )
164
179
165
180
useEffect ( ( ) => {
166
- onErrorRef . current = onError ?? onGlobalError
167
- } , [ onError , onGlobalError ] )
181
+ request . method = ( ) => method ( getParamsRef . current ( ) )
182
+ } , [ method , request ] )
168
183
169
184
useEffect ( ( ) => {
170
185
if ( keepPreviousData ) {
171
186
previousDataRef . current = request . data
172
187
}
173
188
} , [ request . data , keepPreviousData ] )
174
189
190
+ // Reset page when baseParams or pageParamKey change
175
191
useEffect ( ( ) => {
176
- getNextPageRef . current = getNextPage
177
- } , [ getNextPage ] )
178
-
179
- useEffect ( ( ) => {
180
- paramsRef . current =
181
- pageParamKey && page
182
- ? ( { ...baseParams , [ pageParamKey ] : page } as ParamsType )
183
- : baseParams
184
- } , [ baseParams , pageParamKey , page ] )
192
+ setPage ( ( ) => baseParams [ pageParamKey ] )
193
+ nextPageRef . current = baseParams [ pageParamKey ]
194
+ getParamsRef . current = ( ) => ( {
195
+ ...baseParams ,
196
+ [ pageParamKey ] : nextPageRef . current ,
197
+ } )
198
+ } , [ baseParams , pageParamKey ] )
185
199
186
200
useEffect ( ( ) => {
187
201
if ( needLoad ) {
188
- const defaultOnSuccessOrError = ( ) => { }
189
- const onSuccessLoad = onSuccessRef . current ?? defaultOnSuccessOrError
190
- const onFailedLoad = onErrorRef . current ?? defaultOnSuccessOrError
202
+ const onSuccessLoad = getOnSuccessRef . current
203
+ const onFailedLoad = getOnErrorRef . current
191
204
request
192
205
. load ( )
193
206
. then ( async result => {
194
- if ( getNextPageRef . current ) {
195
- nextPageRef . current = getNextPageRef . current (
196
- result ,
197
- paramsRef . current ,
198
- ) as typeof nextPageRef . current
199
- }
207
+ nextPageRef . current = getNextPageFnRef . current (
208
+ result ,
209
+ getParamsRef . current ( ) ,
210
+ ) as typeof page
200
211
await onSuccessLoad ( result )
201
212
} )
202
213
. catch ( onFailedLoad )
203
214
}
204
215
optimisticIsLoadingRef . current = false
205
216
} , [ needLoad , request ] )
206
217
207
- const loadMore = useCallback ( ( ) => {
208
- setPage ( nextPageRef . current )
209
- } , [ ] )
218
+ useEffect ( ( ) => {
219
+ getParamsRef . current = ( ) => ( {
220
+ ...baseParams ,
221
+ [ pageParamKey ] : nextPageRef . current ,
222
+ } )
223
+ } , [ baseParams , pageParamKey ] )
224
+ useEffect ( ( ) => {
225
+ getOnSuccessRef . current = ( ...params ) => onSuccess ?.( ...params )
226
+ } , [ onSuccess ] )
227
+ useEffect ( ( ) => {
228
+ getOnErrorRef . current = err => onError ?.( err ) ?? onGlobalError ?.( err )
229
+ } , [ onError , onGlobalError ] )
230
+ useEffect ( ( ) => {
231
+ getNextPageFnRef . current = ( ...params ) =>
232
+ getNextPage ? getNextPage ( ...params ) : undefined
233
+ } , [ getNextPage ] )
210
234
211
235
const data = useMemo < UseInfiniteDataLoaderResult < ResultType , ErrorType > > (
212
236
( ) => ( {
0 commit comments