Skip to content
Source
Sign inSign up
TanStack/query

fix(query-core): recompute placeholders when select changes #11535

Closedbrennanbutler01(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/11535/headopened View on GitHub

🎯 Changes

Changing or removing select while a query uses placeholder data returns the previous selection. For example, placeholder 2 selected with value * 2 remains 4 after changing the selector to value * 3, instead of becoming 6.

Include selector identity when reusing selected placeholder data. Unchanged selectors retain the existing memoization. Covers constant and functional placeholders, changing/removing/re-adding the selector, and React rerenders with and without Strict Mode.

Related to the optimization in #9007. Reproduced on 5.101.2, stable 5.103.1 source, and main (bb9d313). All four added core/React regression cases failed before the fix. Afterward, 1,599 core assertions and 585 React tests pass.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with pnpm run test:pr, or these tests do not apply to this pull request.
  • I fully understand the code in this pull request, including any code generated with AI assistance.

The full cross-framework test:pr run was not performed. Both affected packages passed test:lib, test:types, test:eslint, and test:build through Nx, including dependency builds. The compiler matrix covers TypeScript 5.6, 5.7, 5.8, 5.9, 6.0, and 7.0. Existing lint warnings are in unchanged code. Tested on Node 24.13.0.

Implemented and locally verified with AI assistance. The contributor-understanding checkbox is left for the author's review.

🚀 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

    • Placeholder data now updates correctly when its selector changes or is removed.
    • Updated selected placeholder values are reflected consistently during query and React component rerenders.
  • Tests

    • Added coverage for selector changes with and without React Strict Mode.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Review Change StackReview Change Stack

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration

      Configuration used: Path: .coderabbit.yaml

      Review profile: CHILL

      Plan: Advanced

      Run ID: a6974f75-f473-458a-aabf-f928a2a64fa2

      📥 Commits

      Reviewing files that changed from the base of the PR and between bb9d31306f4b71fc488c3d3038d6a1c0cde640a1 and 5998b73ee015566f1c9714c09f7bb5283c836e47.

      📒 Files selected for processing (4)
      • .changeset/placeholder-select-fix.md
      • packages/query-core/src/__tests__/queryObserver.test.tsx
      • packages/query-core/src/queryObserver.ts
      • packages/react-query/src/__tests__/useQuery.test.tsx

      Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


      📝 Walkthrough

      Walkthrough

      The query observer now recomputes placeholder data when the select option changes or is removed. Query-core and React tests cover selector updates, unselected values, functional placeholders, and StrictMode rerenders. A patch changeset records the fix.

      Changes

      Placeholder selection

      Layer / File(s)Summary
      Observer memoization and core coverage
      packages/query-core/src/queryObserver.ts, packages/query-core/src/__tests__/queryObserver.test.tsx
      The memoization guard now compares both placeholderData and select. Tests verify values 4, 6, 2, and 8, while isPlaceholderData remains true.
      React integration and release metadata
      packages/react-query/src/__tests__/useQuery.test.tsx, .changeset/placeholder-select-fix.md
      React hook tests cover selector changes and removal during rerenders, including StrictMode. The changeset declares a patch release for @tanstack/query-core.

      Priority: ⬇️ Low

      Estimated code review effort: 2 (Simple) | ~10 minutes

      Change: Bug fix

      Suggested reviewers: sukvvon

      Merge Risk: ⚪ Minimal · up to 5998b

      The placeholder result is recomputed when a selector changes or is removed, with regression coverage for core observers and React rerenders. No actionable merge risk remains.

      🚥 Pre-merge checks | ✅ 5
      ✅ Passed checks (5 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly and concisely describes the primary fix: recomputing placeholder data when the select function changes.
      Description check✅ PassedThe description explains the defect, implementation, regression coverage, validation results, release impact, and changeset. It also identifies that the full test:pr run was not performed and that t…
      Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 3…
      Linked Issues check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes check✅ PassedCheck skipped because no linked issues were found for this pull request.
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create a new PR

      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.
    2. No unresolved blocking issues were identified.

      Pull request overview

      Fixes stale placeholder selections when the query’s select function changes or is removed.

      Changes:

      • Include selector identity in placeholder memoization.
      • Add core and React regression tests.
      • Add a query-core patch changeset.
    3. File summaries
      FileDescription
      packages/react-query/src/__tests__/useQuery.test.tsxTests rerenders with and without Strict Mode.
      packages/query-core/src/queryObserver.tsPrevents stale placeholder selections.
      packages/query-core/src/__tests__/queryObserver.test.tsxAdds core regression coverage.
      .changeset/placeholder-select-fix.mdDocuments the patch release.
      Review details
      • Files reviewed: 4/4 changed files
      • Comments generated: 0
      • Review effort level: Lite

      💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

  • sheraz4196(GitHub profile, opens in a new tab) approved these changes

  • Could someone help me replace this PR from the rewritten branch? I rewrote the commits on my personal fork so the author and committer use my personal GitHub identity. The patch is unchanged, but this PR still shows the old head (5998b73). The rewritten branch is fix/placeholder-select-updates-rewritten (9d11c4e). My personal account can’t create a replacement PR because I’m not a member of the TanStack org. Could someone with org access open a replacement from that branch or tell me the right way to proceed? I’ve left this PR open so the discussion stays here.

  • Superseded by #12564 — same patch, opened from the rewritten branch on my fork. This PR's head ref detached from its branch, so GitHub kept showing the frozen head 5998b73 and a stale conflict even though the branch was rewritten on top of current main. Closing in favor of the replacement.

  • Sign in to join this conversation.