Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/swift-bees-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@scaleway/use-dataloader": minor
---

Fix infinite dataloader effects that reset the page everytime
46 changes: 21 additions & 25 deletions packages/use-dataloader/src/useInfiniteDataLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ export const useInfiniteDataLoader = <
const computedDatalifetime = dataLifetime ?? defaultDatalifetime
const requestRefs = useRef<DataLoader<ResultType, ErrorType>[]>([])
const [page, setPage] = useState(baseParams[pageParamKey])
const nextPageRef = useRef(page)
const nextPageRef = useRef<typeof page | undefined>(page)

const getNextPageFnRef = useRef(
(...params: Parameters<NonNullable<typeof getNextPage>>) =>
getNextPage ? getNextPage(...params) : undefined,
)

const getParamsRef = useRef(() => ({
const paramsRef = useRef({
...baseParams,
[pageParamKey]: nextPageRef.current,
}))
[pageParamKey]: page,
})

const getMethodRef = useRef(() => method(getParamsRef.current()))
const getMethodRef = useRef(() => method(paramsRef.current))

const getOnSuccessRef = useRef(
(...params: Parameters<NonNullable<typeof onSuccess>>) =>
Expand Down Expand Up @@ -174,19 +174,18 @@ export const useInfiniteDataLoader = <
)
}, [])

const loadMore = useCallback(() => {
const nextPage = nextPageRef.current
if (nextPage) {
setPage(() => nextPage)
getParamsRef.current = () => ({
const loadMoreRef = useRef(() => {
if (nextPageRef.current) {
paramsRef.current = {
...baseParams,
[pageParamKey]: nextPage,
})
[pageParamKey]: nextPageRef.current,
}
setPage(curr => nextPageRef.current ?? curr)
}
}, [baseParams, pageParamKey])
})

useEffect(() => {
request.method = () => method(getParamsRef.current())
request.method = () => method(paramsRef.current)
}, [method, request])

useEffect(() => {
Expand All @@ -199,11 +198,8 @@ export const useInfiniteDataLoader = <
useEffect(() => {
setPage(() => baseParams[pageParamKey])
nextPageRef.current = baseParams[pageParamKey]
getParamsRef.current = () => ({
...baseParams,
[pageParamKey]: nextPageRef.current,
})
}, [baseParams, pageParamKey])
/* eslint-disable-next-line react-hooks/exhaustive-deps */
}, [baseQueryKey])

useEffect(() => {
if (needLoad) {
Expand All @@ -214,7 +210,7 @@ export const useInfiniteDataLoader = <
.then(async result => {
nextPageRef.current = getNextPageFnRef.current(
result,
getParamsRef.current(),
paramsRef.current,
) as typeof page
await onSuccessLoad(result)
})
Expand All @@ -224,10 +220,11 @@ export const useInfiniteDataLoader = <
}, [needLoad, request])

useEffect(() => {
getParamsRef.current = () => ({
paramsRef.current = {
...baseParams,
[pageParamKey]: nextPageRef.current,
})
[pageParamKey]: page,
}
/* eslint-disable-next-line react-hooks/exhaustive-deps */
}, [baseParams, pageParamKey])
useEffect(() => {
getOnSuccessRef.current = (...params) => onSuccess?.(...params)
Expand Down Expand Up @@ -258,7 +255,7 @@ export const useInfiniteDataLoader = <
.map(dataloader => dataloader.data) as ResultType[]),
error: request.error,
reload,
loadMore,
loadMore: loadMoreRef.current,
}),
[
initialData,
Expand All @@ -269,7 +266,6 @@ export const useInfiniteDataLoader = <
request.error,
isLoadingFirstPage,
reload,
loadMore,
],
)

Expand Down
Loading