Skip to content

Commit c21e88c

Browse files
committed
fix: use query data when getting next and prev page params
1 parent 9f79d04 commit c21e88c

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/core/infiniteQueryObserver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ export class InfiniteQueryObserver<
107107
...result,
108108
fetchNextPage: this.fetchNextPage,
109109
fetchPreviousPage: this.fetchPreviousPage,
110-
hasNextPage: hasNextPage(this.options, result.data?.pages),
111-
hasPreviousPage: hasPreviousPage(this.options, result.data?.pages),
110+
hasNextPage: hasNextPage(this.options, state.data?.pages),
111+
hasPreviousPage: hasPreviousPage(this.options, state.data?.pages),
112112
isFetchingNextPage:
113113
state.isFetching && state.fetchMeta?.fetchMore?.direction === 'forward',
114114
isFetchingPreviousPage:

src/react/tests/useInfiniteQuery.test.tsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,49 @@ describe('useInfiniteQuery', () => {
969969
})
970970
})
971971

972+
it('should not use selected data when computing hasNextPage', async () => {
973+
const key = queryKey()
974+
const states: UseInfiniteQueryResult<string>[] = []
975+
976+
function Page() {
977+
const state = useInfiniteQuery(
978+
key,
979+
({ pageParam = 1 }) => Number(pageParam),
980+
{
981+
getNextPageParam: lastPage => (lastPage === 1 ? 2 : false),
982+
select: data => ({
983+
pages: data.pages.map(x => x.toString()),
984+
pageParams: data.pageParams,
985+
}),
986+
}
987+
)
988+
989+
states.push(state)
990+
991+
return null
992+
}
993+
994+
renderWithClient(queryClient, <Page />)
995+
996+
await sleep(100)
997+
998+
expect(states.length).toBe(2)
999+
expect(states[0]).toMatchObject({
1000+
data: undefined,
1001+
hasNextPage: undefined,
1002+
isFetching: true,
1003+
isFetchingNextPage: false,
1004+
isSuccess: false,
1005+
})
1006+
expect(states[1]).toMatchObject({
1007+
data: { pages: ['1'] },
1008+
hasNextPage: true,
1009+
isFetching: false,
1010+
isFetchingNextPage: false,
1011+
isSuccess: true,
1012+
})
1013+
})
1014+
9721015
it('should build fresh cursors on refetch', async () => {
9731016
const key = queryKey()
9741017

0 commit comments

Comments
 (0)