Skip to content

Conversation

0xdiid
Copy link

@0xdiid 0xdiid commented Oct 10, 2025

🎯 Changes

This introduces a check when updating dataUpdateCount so that it isn't updated when setting initial data. This in turn fixes the isFetchedAfterMount bug discussed in #9656

fixes #9656

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm test:pr

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Bug Fixes

    • Corrected isFetchedAfterMount when using initial data at mount, preventing it from being incorrectly marked as fetched.
    • Prevented initial data from incrementing update counters; subsequent refetches now update data and counters as expected.
  • Tests

    • Added tests to verify correct behavior with initial data and observer-initiated refetches.
  • Chores

    • Added a patch changeset entry reflecting this bug fix for the package.

Copy link

changeset-bot bot commented Oct 10, 2025

🦋 Changeset detected

Latest commit: ad5d0ba

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@tanstack/react-query Patch
@tanstack/react-query-devtools Patch
@tanstack/react-query-next-experimental Patch
@tanstack/react-query-persist-client Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

coderabbitai bot commented Oct 10, 2025

Walkthrough

This PR adjusts Query’s success-state handling to avoid incrementing dataUpdateCount when initialData is set manually at mount. It adds a targeted test verifying isFetchedAfterMount and dataUpdateCount behavior with initialData and subsequent refetch, and includes a changeset marking a patch for @tanstack/react-query.

Changes

Cohort / File(s) Summary
Core logic update
packages/query-core/src/query.ts
Modify Success action: when action.manual is true and previous state.data is undefined (initial manual data), do not increment dataUpdateCount; otherwise increment as before. Preserve dataUpdatedAt; guard #revertState by action.manual.
Tests
packages/query-core/src/__tests__/query.test.tsx
Add test ensuring initialData via observer does not increment dataUpdateCount and isFetchedAfterMount remains false until an observer-triggered refetch updates both.
Changeset
.changeset/yummy-rooms-read.md
Add patch changeset for @tanstack/react-query describing the bugfix to isFetchedAfterMount with initialData.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor UI as Component
  participant Obs as QueryObserver
  participant QC as QueryClient
  participant Q as Query

  UI->>Obs: Mount with initialData
  Obs->>QC: Subscribe to query key
  QC->>Q: Create/attach observer
  Note right of Q: Apply initialData as manual<br/>(no dataUpdateCount increment,<br/>isFetchedAfterMount=false)

  UI->>Obs: Trigger refetch
  Obs->>Q: fetch()
  Q->>Q: Resolve to fetched data
  Note right of Q: Update data, increment dataUpdateCount,<br/>isFetchedAfterMount=true
  Q-->>Obs: Notify result
  Obs-->>UI: Render with fetched data
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

A hop, a skip, a patch so neat,
My carrots count each data beat. 🥕
First nibble’s manual—no drums sound,
But fetch-time thumps now shake the ground.
Flags flip, counts tick, all set aright—
I nose-twitch, merge, and bound to night.

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title Check ✅ Passed The title succinctly and accurately reflects the main change in the PR—preventing dataUpdateCount updates when setting initial data—and clearly indicates the nature of the fix.
Description Check ✅ Passed The description adheres to the repository template by providing detailed 'Changes', completed 'Checklist', and 'Release Impact' sections with a summary of the implementation, issue reference, testing confirmation, and changeset inclusion.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f4a0cd5 and ad5d0ba.

📒 Files selected for processing (3)
  • .changeset/yummy-rooms-read.md (1 hunks)
  • packages/query-core/src/__tests__/query.test.tsx (1 hunks)
  • packages/query-core/src/query.ts (1 hunks)
🔇 Additional comments (3)
packages/query-core/src/query.ts (1)

639-641: LGTM! Correctly prevents dataUpdateCount increment for initial data.

The conditional logic appropriately identifies when initial data is being set manually at mount (action.manual && state.data === undefined) and preserves the dataUpdateCount in that case. This fix ensures that isFetchedAfterMount remains false until an actual fetch occurs, addressing the bug referenced in issue #9656.

The logic correctly handles subsequent scenarios:

  • Manual data updates after initial data → increments (data no longer undefined)
  • Normal fetches → increments (action.manual is false)
.changeset/yummy-rooms-read.md (1)

1-5: LGTM! Changeset appropriately documents the fix.

The changeset correctly identifies this as a patch-level change and clearly describes the behavioral fix for isFetchedAfterMount when initialData is applied at mount.

packages/query-core/src/__tests__/query.test.tsx (1)

1308-1349: LGTM! Comprehensive test validates the fix.

The test thoroughly validates the expected behavior:

  1. ✓ Confirms dataUpdateCount remains 0 when initialData is set on a prefetched query
  2. ✓ Verifies isFetchedAfterMount is false with only initial data
  3. ✓ Confirms dataUpdateCount increments to 1 after an actual refetch
  4. ✓ Verifies isFetchedAfterMount becomes true after the fetch

This directly addresses the bug scenario and provides good regression coverage.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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

Successfully merging this pull request may close these issues.

isFetchedAfterMount was broken in commit 1c8a921 / v5.87.1
1 participant