@@ -48,19 +48,19 @@ export const useInfiniteDataLoader = <
4848 const computedDatalifetime = dataLifetime ?? defaultDatalifetime
4949 const requestRefs = useRef < DataLoader < ResultType , ErrorType > [ ] > ( [ ] )
5050 const [ page , setPage ] = useState ( baseParams [ pageParamKey ] )
51- const nextPageRef = useRef ( page )
51+ const nextPageRef = useRef < typeof page | undefined > ( page )
5252
5353 const getNextPageFnRef = useRef (
5454 ( ...params : Parameters < NonNullable < typeof getNextPage > > ) =>
5555 getNextPage ? getNextPage ( ...params ) : undefined ,
5656 )
5757
58- const getParamsRef = useRef ( ( ) => ( {
58+ const paramsRef = useRef ( {
5959 ...baseParams ,
60- [ pageParamKey ] : nextPageRef . current ,
61- } ) )
60+ [ pageParamKey ] : page ,
61+ } )
6262
63- const getMethodRef = useRef ( ( ) => method ( getParamsRef . current ( ) ) )
63+ const getMethodRef = useRef ( ( ) => method ( paramsRef . current ) )
6464
6565 const getOnSuccessRef = useRef (
6666 ( ...params : Parameters < NonNullable < typeof onSuccess > > ) =>
@@ -174,19 +174,18 @@ export const useInfiniteDataLoader = <
174174 )
175175 } , [ ] )
176176
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 = {
182180 ...baseParams ,
183- [ pageParamKey ] : nextPage ,
184- } )
181+ [ pageParamKey ] : nextPageRef . current ,
182+ }
183+ setPage ( curr => nextPageRef . current ?? curr )
185184 }
186- } , [ baseParams , pageParamKey ] )
185+ } )
187186
188187 useEffect ( ( ) => {
189- request . method = ( ) => method ( getParamsRef . current ( ) )
188+ request . method = ( ) => method ( paramsRef . current )
190189 } , [ method , request ] )
191190
192191 useEffect ( ( ) => {
@@ -199,11 +198,8 @@ export const useInfiniteDataLoader = <
199198 useEffect ( ( ) => {
200199 setPage ( ( ) => baseParams [ pageParamKey ] )
201200 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 ] )
207203
208204 useEffect ( ( ) => {
209205 if ( needLoad ) {
@@ -214,7 +210,7 @@ export const useInfiniteDataLoader = <
214210 . then ( async result => {
215211 nextPageRef . current = getNextPageFnRef . current (
216212 result ,
217- getParamsRef . current ( ) ,
213+ paramsRef . current ,
218214 ) as typeof page
219215 await onSuccessLoad ( result )
220216 } )
@@ -224,10 +220,11 @@ export const useInfiniteDataLoader = <
224220 } , [ needLoad , request ] )
225221
226222 useEffect ( ( ) => {
227- getParamsRef . current = ( ) => ( {
223+ paramsRef . current = {
228224 ...baseParams ,
229- [ pageParamKey ] : nextPageRef . current ,
230- } )
225+ [ pageParamKey ] : page ,
226+ }
227+ /* eslint-disable-next-line react-hooks/exhaustive-deps */
231228 } , [ baseParams , pageParamKey ] )
232229 useEffect ( ( ) => {
233230 getOnSuccessRef . current = ( ...params ) => onSuccess ?.( ...params )
@@ -258,7 +255,7 @@ export const useInfiniteDataLoader = <
258255 . map ( dataloader => dataloader . data ) as ResultType [ ] ) ,
259256 error : request . error ,
260257 reload,
261- loadMore,
258+ loadMore : loadMoreRef . current ,
262259 } ) ,
263260 [
264261 initialData ,
@@ -269,7 +266,6 @@ export const useInfiniteDataLoader = <
269266 request . error ,
270267 isLoadingFirstPage ,
271268 reload ,
272- loadMore ,
273269 ] ,
274270 )
275271
0 commit comments