@@ -48,19 +48,19 @@ export const useInfiniteDataLoader = <
48
48
const computedDatalifetime = dataLifetime ?? defaultDatalifetime
49
49
const requestRefs = useRef < DataLoader < ResultType , ErrorType > [ ] > ( [ ] )
50
50
const [ page , setPage ] = useState ( baseParams [ pageParamKey ] )
51
- const nextPageRef = useRef ( page )
51
+ const nextPageRef = useRef < typeof page | undefined > ( page )
52
52
53
53
const getNextPageFnRef = useRef (
54
54
( ...params : Parameters < NonNullable < typeof getNextPage > > ) =>
55
55
getNextPage ? getNextPage ( ...params ) : undefined ,
56
56
)
57
57
58
- const getParamsRef = useRef ( ( ) => ( {
58
+ const paramsRef = useRef ( {
59
59
...baseParams ,
60
- [ pageParamKey ] : nextPageRef . current ,
61
- } ) )
60
+ [ pageParamKey ] : page ,
61
+ } )
62
62
63
- const getMethodRef = useRef ( ( ) => method ( getParamsRef . current ( ) ) )
63
+ const getMethodRef = useRef ( ( ) => method ( paramsRef . current ) )
64
64
65
65
const getOnSuccessRef = useRef (
66
66
( ...params : Parameters < NonNullable < typeof onSuccess > > ) =>
@@ -174,19 +174,18 @@ export const useInfiniteDataLoader = <
174
174
)
175
175
} , [ ] )
176
176
177
- const loadMore = useCallback ( ( ) => {
178
- const nextPage = nextPageRef . current
179
- if ( nextPage ) {
180
- setPage ( ( ) => nextPage )
181
- getParamsRef . current = ( ) => ( {
177
+ const loadMoreRef = useRef ( ( ) => {
178
+ if ( nextPageRef . current ) {
179
+ paramsRef . current = {
182
180
...baseParams ,
183
- [ pageParamKey ] : nextPage ,
184
- } )
181
+ [ pageParamKey ] : nextPageRef . current ,
182
+ }
183
+ setPage ( curr => nextPageRef . current ?? curr )
185
184
}
186
- } , [ baseParams , pageParamKey ] )
185
+ } )
187
186
188
187
useEffect ( ( ) => {
189
- request . method = ( ) => method ( getParamsRef . current ( ) )
188
+ request . method = ( ) => method ( paramsRef . current )
190
189
} , [ method , request ] )
191
190
192
191
useEffect ( ( ) => {
@@ -199,11 +198,8 @@ export const useInfiniteDataLoader = <
199
198
useEffect ( ( ) => {
200
199
setPage ( ( ) => baseParams [ pageParamKey ] )
201
200
nextPageRef . current = baseParams [ pageParamKey ]
202
- getParamsRef . current = ( ) => ( {
203
- ...baseParams ,
204
- [ pageParamKey ] : nextPageRef . current ,
205
- } )
206
- } , [ baseParams , pageParamKey ] )
201
+ /* eslint-disable-next-line react-hooks/exhaustive-deps */
202
+ } , [ baseQueryKey ] )
207
203
208
204
useEffect ( ( ) => {
209
205
if ( needLoad ) {
@@ -214,7 +210,7 @@ export const useInfiniteDataLoader = <
214
210
. then ( async result => {
215
211
nextPageRef . current = getNextPageFnRef . current (
216
212
result ,
217
- getParamsRef . current ( ) ,
213
+ paramsRef . current ,
218
214
) as typeof page
219
215
await onSuccessLoad ( result )
220
216
} )
@@ -224,10 +220,11 @@ export const useInfiniteDataLoader = <
224
220
} , [ needLoad , request ] )
225
221
226
222
useEffect ( ( ) => {
227
- getParamsRef . current = ( ) => ( {
223
+ paramsRef . current = {
228
224
...baseParams ,
229
- [ pageParamKey ] : nextPageRef . current ,
230
- } )
225
+ [ pageParamKey ] : page ,
226
+ }
227
+ /* eslint-disable-next-line react-hooks/exhaustive-deps */
231
228
} , [ baseParams , pageParamKey ] )
232
229
useEffect ( ( ) => {
233
230
getOnSuccessRef . current = ( ...params ) => onSuccess ?.( ...params )
@@ -258,7 +255,7 @@ export const useInfiniteDataLoader = <
258
255
. map ( dataloader => dataloader . data ) as ResultType [ ] ) ,
259
256
error : request . error ,
260
257
reload,
261
- loadMore,
258
+ loadMore : loadMoreRef . current ,
262
259
} ) ,
263
260
[
264
261
initialData ,
@@ -269,7 +266,6 @@ export const useInfiniteDataLoader = <
269
266
request . error ,
270
267
isLoadingFirstPage ,
271
268
reload ,
272
- loadMore ,
273
269
] ,
274
270
)
275
271
0 commit comments