Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add infinite query skip support #4906

Merged
merged 1 commit into from
Mar 30, 2025
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
11 changes: 6 additions & 5 deletions packages/toolkit/src/query/react/buildHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1535,16 +1535,17 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
const { endpointName } = lastResult
const endpointDefinition = context.endpointDefinitions[endpointName]
if (
queryArgs !== skipToken &&
serializeQueryArgs({
queryArgs: lastResult.originalArgs,
endpointDefinition,
endpointName,
}) ===
serializeQueryArgs({
queryArgs,
endpointDefinition,
endpointName,
})
serializeQueryArgs({
queryArgs,
endpointDefinition,
endpointName,
})
)
lastResult = undefined
}
Expand Down
35 changes: 35 additions & 0 deletions packages/toolkit/src/query/tests/buildHooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2216,6 +2216,41 @@ describe('hooks tests', () => {
expect(totalRenderCount()).toBe(3)
expect(numRequests).toBe(1)
})

test('useInfiniteQuery hook does not fetch when the skip token is set', async () => {
function Pokemon() {
const [value, setValue] = useState(0)

const { isFetching } = pokemonApi.useGetInfinitePokemonInfiniteQuery(
'fire',
{
skip: value < 1,
},
)
getRenderCount = useRenderCounter()

return (
<div>
<div data-testid="isFetching">{String(isFetching)}</div>
<button onClick={() => setValue((val) => val + 1)}>
Increment value
</button>
</div>
)
}

render(<Pokemon />, { wrapper: storeRef.wrapper })
expect(getRenderCount()).toBe(1)

await waitFor(() =>
expect(screen.getByTestId('isFetching').textContent).toBe('false'),
)
fireEvent.click(screen.getByText('Increment value'))
await waitFor(() =>
expect(screen.getByTestId('isFetching').textContent).toBe('true'),
)
expect(getRenderCount()).toBe(2)
})
})

describe('useMutation', () => {
Expand Down