@@ -55,13 +55,12 @@ export const useInfiniteDataLoader = <
5555 getNextPage ? getNextPage ( ...params ) : undefined ,
5656 )
5757
58- const paramsRef = useRef ( {
58+ const paramsArgs = {
5959 ...baseParams ,
6060 [ pageParamKey ] : page ,
61- } )
62-
63- const getMethodRef = useRef ( ( ) => method ( paramsRef . current ) )
61+ }
6462
63+ const getMethodRef = useRef ( ( ) => method ( paramsArgs ) )
6564 const getOnSuccessRef = useRef (
6665 ( ...params : Parameters < NonNullable < typeof onSuccess > > ) =>
6766 onSuccess ?.( ...params ) ,
@@ -99,23 +98,28 @@ export const useInfiniteDataLoader = <
9998 'infinite' ,
10099 page as string | number ,
101100 ] )
101+
102102 // Clean bad requests in the array
103103 requestRefs . current = requestRefs . current . filter ( request => {
104104 if ( request . key . startsWith ( computeKey ( baseQueryKey ) ) ) {
105105 return true
106106 }
107+
107108 request . removeObserver ( forceRerender . current )
108109
109110 return false
110111 } )
112+
111113 const requestInRef = requestRefs . current . find ( request =>
112114 request . key . endsWith ( currentQueryKey ) ,
113115 )
116+
114117 if ( ! requestInRef ) {
115118 const request = getOrAddRequest < ResultType , ErrorType > ( currentQueryKey , {
116119 enabled,
117120 method : getMethodRef . current ,
118121 } )
122+
119123 if ( ! request . observers . includes ( forceRerender . current ) ) {
120124 request . addObserver ( forceRerender . current )
121125 }
@@ -176,16 +180,13 @@ export const useInfiniteDataLoader = <
176180
177181 const loadMoreRef = useRef ( ( ) => {
178182 if ( nextPageRef . current ) {
179- paramsRef . current = {
180- ...baseParams ,
181- [ pageParamKey ] : nextPageRef . current ,
182- }
183183 setPage ( curr => nextPageRef . current ?? curr )
184184 }
185185 } )
186186
187187 useEffect ( ( ) => {
188- request . method = ( ) => method ( paramsRef . current )
188+ request . method = ( ) => method ( paramsArgs )
189+ // eslint-disable-next-line react-hooks/exhaustive-deps
189190 } , [ method , request ] )
190191
191192 useEffect ( ( ) => {
@@ -210,28 +211,24 @@ export const useInfiniteDataLoader = <
210211 . then ( async result => {
211212 nextPageRef . current = getNextPageFnRef . current (
212213 result ,
213- paramsRef . current ,
214+ paramsArgs ,
214215 ) as typeof page
215216 await onSuccessLoad ( result )
216217 } )
217218 . catch ( onFailedLoad )
218219 }
219220 optimisticIsLoadingRef . current = false
221+ // eslint-disable-next-line react-hooks/exhaustive-deps
220222 } , [ needLoad , request ] )
221223
222- useEffect ( ( ) => {
223- paramsRef . current = {
224- ...baseParams ,
225- [ pageParamKey ] : page ,
226- }
227- /* eslint-disable-next-line react-hooks/exhaustive-deps */
228- } , [ baseParams , pageParamKey ] )
229224 useEffect ( ( ) => {
230225 getOnSuccessRef . current = ( ...params ) => onSuccess ?.( ...params )
231226 } , [ onSuccess ] )
227+
232228 useEffect ( ( ) => {
233229 getOnErrorRef . current = err => onError ?.( err ) ?? onGlobalError ?.( err )
234230 } , [ onError , onGlobalError ] )
231+
235232 useEffect ( ( ) => {
236233 getNextPageFnRef . current = ( ...params ) =>
237234 getNextPage ? getNextPage ( ...params ) : undefined
0 commit comments