Skip to content

Commit a6ef6be

Browse files
committed
fix: bug on infinity dataloader
1 parent 018f235 commit a6ef6be

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

.changeset/spotty-words-flow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@scaleway/use-dataloader": patch
3+
---
4+
5+
Fix bug on infinity data loader

packages/fuzzy-search/src/__tests__/index.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, test } from 'vitest'
2-
import { isFuzzyMatch, levenshteinDistance, normalizeString } from ".."
2+
import { isFuzzyMatch, levenshteinDistance, normalizeString } from '..'
33

44
describe('fuzzySearch', () => {
55
describe('normalizeString', () => {

packages/use-dataloader/src/useInfiniteDataLoader.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ export const useInfiniteDataLoader = <
5555
getNextPage ? getNextPage(...params) : undefined,
5656
)
5757

58-
const paramsRef = useRef({
59-
...baseParams,
60-
[pageParamKey]: page,
61-
})
62-
63-
const getMethodRef = useRef(() => method(paramsRef.current))
58+
const paramsMemo = useMemo(
59+
() => ({
60+
...baseParams,
61+
[pageParamKey]: page,
62+
}),
63+
[baseParams, page, pageParamKey],
64+
)
6465

66+
const getMethodRef = useRef(() => method(paramsMemo))
6567
const getOnSuccessRef = useRef(
6668
(...params: Parameters<NonNullable<typeof onSuccess>>) =>
6769
onSuccess?.(...params),
@@ -99,23 +101,28 @@ export const useInfiniteDataLoader = <
99101
'infinite',
100102
page as string | number,
101103
])
104+
102105
// Clean bad requests in the array
103106
requestRefs.current = requestRefs.current.filter(request => {
104107
if (request.key.startsWith(computeKey(baseQueryKey))) {
105108
return true
106109
}
110+
107111
request.removeObserver(forceRerender.current)
108112

109113
return false
110114
})
115+
111116
const requestInRef = requestRefs.current.find(request =>
112117
request.key.endsWith(currentQueryKey),
113118
)
119+
114120
if (!requestInRef) {
115121
const request = getOrAddRequest<ResultType, ErrorType>(currentQueryKey, {
116122
enabled,
117123
method: getMethodRef.current,
118124
})
125+
119126
if (!request.observers.includes(forceRerender.current)) {
120127
request.addObserver(forceRerender.current)
121128
}
@@ -176,17 +183,13 @@ export const useInfiniteDataLoader = <
176183

177184
const loadMoreRef = useRef(() => {
178185
if (nextPageRef.current) {
179-
paramsRef.current = {
180-
...baseParams,
181-
[pageParamKey]: nextPageRef.current,
182-
}
183186
setPage(curr => nextPageRef.current ?? curr)
184187
}
185188
})
186189

187190
useEffect(() => {
188-
request.method = () => method(paramsRef.current)
189-
}, [method, request])
191+
request.method = () => method(paramsMemo)
192+
}, [method, request, paramsMemo])
190193

191194
useEffect(() => {
192195
if (keepPreviousData) {
@@ -210,28 +213,23 @@ export const useInfiniteDataLoader = <
210213
.then(async result => {
211214
nextPageRef.current = getNextPageFnRef.current(
212215
result,
213-
paramsRef.current,
216+
paramsMemo,
214217
) as typeof page
215218
await onSuccessLoad(result)
216219
})
217220
.catch(onFailedLoad)
218221
}
219222
optimisticIsLoadingRef.current = false
220-
}, [needLoad, request])
223+
}, [needLoad, request, paramsMemo])
221224

222-
useEffect(() => {
223-
paramsRef.current = {
224-
...baseParams,
225-
[pageParamKey]: page,
226-
}
227-
/* eslint-disable-next-line react-hooks/exhaustive-deps */
228-
}, [baseParams, pageParamKey])
229225
useEffect(() => {
230226
getOnSuccessRef.current = (...params) => onSuccess?.(...params)
231227
}, [onSuccess])
228+
232229
useEffect(() => {
233230
getOnErrorRef.current = err => onError?.(err) ?? onGlobalError?.(err)
234231
}, [onError, onGlobalError])
232+
235233
useEffect(() => {
236234
getNextPageFnRef.current = (...params) =>
237235
getNextPage ? getNextPage(...params) : undefined

0 commit comments

Comments
 (0)