Skip to content

fix(query): prevent onQueryStarted from triggering at end-of-list#5182

Open
joseph0926 wants to merge 2 commits into
reduxjs:masterfrom
joseph0926:fix/infinite-query-onQueryStarted-end-of-list
Open

fix(query): prevent onQueryStarted from triggering at end-of-list#5182
joseph0926 wants to merge 2 commits into
reduxjs:masterfrom
joseph0926:fix/infinite-query-onQueryStarted-end-of-list

Conversation

@joseph0926

Copy link
Copy Markdown

Fixes #5167

Summary

When fetchNextPage() or fetchPreviousPage() is called at end-of-list (when getNextPageParam/getPreviousPageParam returns undefined), the thunk was still executing and triggering onQueryStarted with stale cached data.

To solve the problem added an early bail-out in the thunk's condition function -> to check if the next/previous page actually exists before processing any action.

Changes

  • Added direction check in buildThunks.ts condition function
  • Added test case for the fix
  • Updated existing test expectation (removed isFetchingPreviousPage: true when no previous page exists)

Test

  • yarn test
  • yarn type-tests
  • yarn eslint

@codesandbox

codesandbox Bot commented Jan 15, 2026

Copy link
Copy Markdown

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

@codesandbox-ci

codesandbox-ci Bot commented Jan 15, 2026

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 475a8e3:

Sandbox Source
@examples-query-react/basic Configuration
@examples-query-react/advanced Configuration
@examples-action-listener/counter Configuration
rtk-esm-cra Configuration

@netlify

netlify Bot commented Jan 15, 2026

Copy link
Copy Markdown

Deploy Preview for redux-starter-kit-docs ready!

Name Link
🔨 Latest commit 475a8e3
🔍 Latest deploy log https://app.netlify.com/projects/redux-starter-kit-docs/deploys/696f6186922ed80008fea6b0
😎 Deploy Preview https://deploy-preview-5182--redux-starter-kit-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@aryaemami59 aryaemami59 added the RTK-Query Issues related to Redux-Toolkit-Query label Jan 20, 2026
@joseph0926

Copy link
Copy Markdown
Author

475a8e3

The tests added in the previous commit seemed slightly insufficient, so I added additional test code for possible edge cases.

For example, while only forward testing existed before, this commit adds verification for backward cases, as well as handling for skip operations and null values....

@aryaemami59 aryaemami59 changed the title fix(query): prevent onQueryStarted from triggering at end-of-list fix(query): prevent onQueryStarted from triggering at end-of-list Mar 18, 2026

@themavik themavik left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gating initiate when getNextPageParam/getPreviousPageParam returns null stops the extra onQueryStarted at the tail—matches the new tests. nit: the guard only runs when cachedData.pages.length is truthy; if an infinite query ever has a zero-page cached shape you might still dispatch—probably unreachable, but worth a glance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

RTK-Query Issues related to Redux-Toolkit-Query

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fetchNextPage always triggering onQueryStarted, even when queryFulfilled already occured.

3 participants