Skip to content

Commit

Permalink
fix(core): make sure infiniteQuery always fetches the first page (#8051)
Browse files Browse the repository at this point in the history
The pageParam == null bailout is meant for fetching further pages, where users can return null/undefined from getNextPageParam to stop fetching; However, after the latest refactoring, we also did this for the initial fetch. This stops fetching from working at all if the initialPageParam was set to null/undefined, which I didn't think anyone would do, but here we are.
  • Loading branch information
TkDodo committed Sep 12, 2024
1 parent 666c8b2 commit 7624785
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
28 changes: 28 additions & 0 deletions packages/query-core/src/__tests__/infiniteQueryBehavior.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,32 @@ describe('InfiniteQueryBehavior', () => {

expect(reFetchedData.data?.pageParams).toEqual([1, 2, 3])
})

test('should fetch even if initialPageParam is null', async () => {
const key = queryKey()

const observer = new InfiniteQueryObserver(queryClient, {
queryKey: key,
queryFn: async () => 'data',
getNextPageParam: () => null,
initialPageParam: null,
})

let observerResult:
| InfiniteQueryObserverResult<unknown, unknown>
| undefined

const unsubscribe = observer.subscribe((result) => {
observerResult = result
})

await waitFor(() =>
expect(observerResult).toMatchObject({
isFetching: false,
data: { pages: ['data'], pageParams: [null] },
}),
)

unsubscribe()
})
})
2 changes: 1 addition & 1 deletion packages/query-core/src/infiniteQueryBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function infiniteQueryBehavior<TQueryFnData, TError, TData, TPageParam>(
currentPage === 0
? (oldPageParams[0] ?? options.initialPageParam)
: getNextPageParam(options, result)
if (param == null) {
if (currentPage > 0 && param == null) {
break
}
result = await fetchPage(result, param)
Expand Down

0 comments on commit 7624785

Please sign in to comment.