diff --git a/packages/toolkit/src/query/core/apiState.ts b/packages/toolkit/src/query/core/apiState.ts index 26647ce41f..ca9d2c1833 100644 --- a/packages/toolkit/src/query/core/apiState.ts +++ b/packages/toolkit/src/query/core/apiState.ts @@ -45,6 +45,7 @@ export type GetPreviousPageParamFunction = ( ) => TPageParam | undefined | null export type InfiniteQueryConfigOptions = { + initialPageParam: TPageParam /** * This function can be set to automatically get the previous cursor for infinite queries. * The result will also be used to determine the value of `hasPreviousPage`. diff --git a/packages/toolkit/src/query/core/buildThunks.ts b/packages/toolkit/src/query/core/buildThunks.ts index d775c3c9fc..6d3ac0a216 100644 --- a/packages/toolkit/src/query/core/buildThunks.ts +++ b/packages/toolkit/src/query/core/buildThunks.ts @@ -561,7 +561,8 @@ export function buildThunks< // Fetch first page result = await fetchPage( existingData, - existingData.pageParams[0] ?? arg.originalArgs, + existingData.pageParams[0] ?? + endpointDefinition.infiniteQueryOptions.initialPageParam, // arg.originalArgs, ) //original diff --git a/packages/toolkit/src/query/endpointDefinitions.ts b/packages/toolkit/src/query/endpointDefinitions.ts index e37366d348..260574819a 100644 --- a/packages/toolkit/src/query/endpointDefinitions.ts +++ b/packages/toolkit/src/query/endpointDefinitions.ts @@ -601,7 +601,7 @@ export interface InfiniteQueryExtraOptions< */ invalidatesTags?: never - infiniteQueryOptions: InfiniteQueryConfigOptions + infiniteQueryOptions: InfiniteQueryConfigOptions /** * Can be provided to return a custom cache key value based on the query arguments. diff --git a/packages/toolkit/src/query/react/buildHooks.ts b/packages/toolkit/src/query/react/buildHooks.ts index befc6139c4..30ad40cc32 100644 --- a/packages/toolkit/src/query/react/buildHooks.ts +++ b/packages/toolkit/src/query/react/buildHooks.ts @@ -1943,6 +1943,7 @@ export function buildHooks({ const queryStateResults = useInfiniteQueryState( arg, { + initialPageParam: options?.initialPageParam!, getNextPageParam: options?.getNextPageParam!, getPreviousPageParam: options?.getPreviousPageParam, }, diff --git a/packages/toolkit/src/query/tests/buildHooks.test.tsx b/packages/toolkit/src/query/tests/buildHooks.test.tsx index 6f9f454493..afa0c88169 100644 --- a/packages/toolkit/src/query/tests/buildHooks.test.tsx +++ b/packages/toolkit/src/query/tests/buildHooks.test.tsx @@ -783,6 +783,7 @@ describe('hooks tests', () => { endpoints: (builder) => ({ getInfinitePokemon: builder.infiniteQuery({ infiniteQueryOptions: { + initialPageParam: 0, getNextPageParam: ( lastPage, allPages, @@ -813,6 +814,7 @@ describe('hooks tests', () => { function User() { const { data, isFetching, isUninitialized, fetchNextPage } = pokemonApi.endpoints.getInfinitePokemon.useInfiniteQuery('a', { + initialPageParam: 0, getNextPageParam: (lastPageParam) => lastPageParam + 1, })