Skip to content
Source
Sign inSign up
TanStack/query

fix(query-core): fix combine not updating when queries change with stable reference #9954

Mergedminseong0324(GitHub profile, opens in a new tab)merged 6 commits intomain fromrefs/pull/9954/headmerged by TkDodo View on GitHub

🎯 Changes

Fixes #8781

Problem

With stable combine, results lag one step behind when queries change. This happens because getOptimisticResult runs before setQueries updates this.#result.

Solution

Pass queryHashes to #combineResult to detect query changes before this.#result is updated.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run 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

    • Combined query results now reliably update when the set of queries changes (add/remove/replace), ensuring aggregated results stay accurate.
  • Tests

    • Added coverage for dynamic query counts and stable combine behavior to prevent regressions.
  • Chores

    • Release patch entry added documenting the fix.
✏️ Tip: You can customize this high-level summary in your review settings.
    1. changeset-bot[bot](GitHub profile, opens in a new tab)commented

      🦋 Changeset detected

      Latest commit: 4c928c0ac752a318d7b4c91dc4c18e34b6173324

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

      This PR includes changesets to release 19 packages
      NameType
      @tanstack/query-corePatch
      @tanstack/angular-query-experimentalPatch
      @tanstack/query-async-storage-persisterPatch
      @tanstack/query-broadcast-client-experimentalPatch
      @tanstack/query-persist-client-corePatch
      @tanstack/query-sync-storage-persisterPatch
      @tanstack/react-queryPatch
      @tanstack/solid-queryPatch
      @tanstack/svelte-queryPatch
      @tanstack/vue-queryPatch
      @tanstack/angular-query-persist-clientPatch
      @tanstack/react-query-persist-clientPatch
      @tanstack/solid-query-persist-clientPatch
      @tanstack/svelte-query-persist-clientPatch
      @tanstack/react-query-devtoolsPatch
      @tanstack/react-query-next-experimentalPatch
      @tanstack/solid-query-devtoolsPatch
      @tanstack/svelte-query-devtoolsPatch
      @tanstack/vue-query-devtoolsPatch

      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

    2. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      📝 Walkthrough

      Walkthrough

      Adds tracking of query hashes in QueriesObserver to detect structural changes and force re-evaluation of a stable combine function; updates optimistic result flow and tests to cover dynamic add/remove/replace scenarios and duplicate keys.

      Changes

      Cohort / File(s)Summary
      Changeset
      \.changeset/swift-brooms-teach.md``
      New changeset added: patch bump for @tanstack/query-core with description "fix stable combine reference not updating when queries change dynamically."
      Query Core Implementation
      \packages/query-core/src/queriesObserver.ts``
      Adds private #lastQueryHashes?: string[]; threads query-hash computation through getOptimisticResult into #combineResult(queryHashes?); compares and updates #lastQueryHashes to trigger recompute when hashes change. Method signature of #combineResult updated.
      Query Core Tests
      \packages/query-core/src/tests/queriesObserver.test.tsx``
      Adds tests exercising optimistic results and stable combine behavior across adding, removing, replacing queries, duplicate keys, and early-return update scenarios.
      React Query Tests
      \packages/react-query/src/tests/useQueries.test.tsx``
      Adds a test ensuring useQueries with a stable combine reference returns correct results as the number of queries changes.

      Estimated code review effort

      🎯 4 (Complex) | ⏱️ ~45 minutes

      • TanStack/query#9592 — Related changes ensuring memoized combine re-runs when queries or their results change (per-index and overall change-detection overlap).
      • TanStack/query#9639 — Related refinements to queriesObserver change-detection and observer reconfiguration to avoid incorrect/infinite updates.
      • TanStack/query#9886 — Additional modifications around queryHash handling and observer matching in QueriesObserver.

      Suggested reviewers

      • TkDodo

      Poem

      🐰 I tracked the hashes, one by one, So combines wake when queries run. Adding, swapping, left or right, The results now hop into sight. A cheeky fix — makes everything fun! 🥕✨

      Pre-merge checks and finishing touches

      ❌ Failed checks (1 warning)
      Check nameStatusExplanationResolution
      Docstring Coverage⚠️ WarningDocstring coverage is 0.00% which is insufficient. The required threshold is 80.00%.You can run @coderabbitai generate docstrings to improve docstring coverage.
      ✅ Passed checks (4 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title accurately summarizes the main fix: ensuring the combine function updates when queries change with a stable reference.
      Description check✅ PassedThe description follows the template, explains the problem and solution clearly, and includes all required checklist items completed.
      Linked Issues check✅ PassedThe PR successfully addresses issue #8781 by detecting query changes via queryHashes before result updates, preventing the one-render lag with stable combine references.
      Out of Scope Changes check✅ PassedAll changes are scoped to fixing the stable combine reference bug: test additions, queryHashes tracking implementation, and changeset file.
      ✨ Finishing touches
      • 📝 Generate docstrings

      📜 Recent review details

      Configuration used: defaults

      Review profile: CHILL

      Plan: Pro

      📥 Commits

      Reviewing files that changed from the base of the PR and between 89db791104f4435887de45f9eb4ffdd07da0bdb9 and 291afeca865cf86b6f415651e06a644fb9677c72.

      📒 Files selected for processing (1)
      • packages/query-core/src/queriesObserver.ts
      🧰 Additional context used
      🧬 Code graph analysis (1)
      packages/query-core/src/queriesObserver.ts (1)
      packages/query-core/src/types.ts (1)
      • QueryObserverResult (903-908)
      ⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
      • GitHub Check: Preview
      • GitHub Check: Test
      🔇 Additional comments (4)
      packages/query-core/src/queriesObserver.ts (4)

      46-46: LGTM!

      The new #lastQueryHashes field follows the naming convention of other caching fields (#lastCombine, #lastResult) and is appropriately typed as optional.


      184-191: LGTM! Correctly captures query state before #result is updated.

      Computing queryHashes from the freshly matched observers and passing them to #combineResult ensures that structural changes to the queries array are detected immediately during the optimistic result phase, before setQueries updates this.#result.


      222-240: LGTM! The query hash comparison logic correctly detects structural changes.

      The implementation properly handles the key scenarios:

      1. First call: #lastQueryHashes is undefined → queryHashesChanged is false, but #combinedResult is also undefined, so recomputation happens anyway.
      2. Subsequent optimistic calls: Both hash arrays are defined, enabling detection of query additions/removals/replacements.
      3. #notify() path: Called without queryHashes, so #lastQueryHashes is preserved. This is correct since #notify() runs after setQueries has already updated #result, and the this.#result !== this.#lastResult check handles that path.

      295-309: Verify the intentional asymmetry between update paths.

      The #notify() method calls #combineResult without queryHashes, which is correct because:

      1. By the time #notify() executes, setQueries has already updated this.#result
      2. Changes are detected via this.#result !== this.#lastResult

      This intentional asymmetry preserves #lastQueryHashes for subsequent optimistic calls. Just confirming this design choice is understood for future maintainers.


      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.
    3. github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: react-query

    4. github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: query-core

    5. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit 4c928c0ac752a318d7b4c91dc4c18e34b6173324

      CommandStatusDurationResult
      nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded56sView ↗

      ☁️ Nx Cloud last updated this comment at 2026-01-16 15:24:20 UTC

    6. pkg-pr-new[bot](GitHub profile, opens in a new tab)commented
      More templates

      @tanstack/angular-query-experimental

      npm i https://pkg.pr.new/@tanstack/angular-query-experimental@9954

      @tanstack/eslint-plugin-query

      npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@9954

      @tanstack/query-async-storage-persister

      npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@9954

      @tanstack/query-broadcast-client-experimental

      npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@9954

      @tanstack/query-core

      npm i https://pkg.pr.new/@tanstack/query-core@9954

      @tanstack/query-devtools

      npm i https://pkg.pr.new/@tanstack/query-devtools@9954

      @tanstack/query-persist-client-core

      npm i https://pkg.pr.new/@tanstack/query-persist-client-core@9954

      @tanstack/query-sync-storage-persister

      npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@9954

      @tanstack/react-query

      npm i https://pkg.pr.new/@tanstack/react-query@9954

      @tanstack/react-query-devtools

      npm i https://pkg.pr.new/@tanstack/react-query-devtools@9954

      @tanstack/react-query-next-experimental

      npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@9954

      @tanstack/react-query-persist-client

      npm i https://pkg.pr.new/@tanstack/react-query-persist-client@9954

      @tanstack/solid-query

      npm i https://pkg.pr.new/@tanstack/solid-query@9954

      @tanstack/solid-query-devtools

      npm i https://pkg.pr.new/@tanstack/solid-query-devtools@9954

      @tanstack/solid-query-persist-client

      npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@9954

      @tanstack/svelte-query

      npm i https://pkg.pr.new/@tanstack/svelte-query@9954

      @tanstack/svelte-query-devtools

      npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@9954

      @tanstack/svelte-query-persist-client

      npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@9954

      @tanstack/vue-query

      npm i https://pkg.pr.new/@tanstack/vue-query@9954

      @tanstack/vue-query-devtools

      npm i https://pkg.pr.new/@tanstack/vue-query-devtools@9954

      commit: 4c928c0

    7. codecov[bot](GitHub profile, opens in a new tab)commented

      Codecov Report

      :white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 59.84%. Comparing base (0d07172) to head (4c928c0). :warning: Report is 1 commits behind head on main.

      Additional details and impacted files

      Impacted file tree graph

      @@             Coverage Diff             @@
      ##             main    #9954       +/-   ##
      ===========================================
      + Coverage   45.83%   59.84%   +14.01%     
      ===========================================
        Files         200      129       -71     
        Lines        8524     5748     -2776     
        Branches     1978     1590      -388     
      ===========================================
      - Hits         3907     3440      -467     
      + Misses       4157     1993     -2164     
      + Partials      460      315      -145     
      ComponentsCoverage Δ
      @tanstack/angular-query-experimental93.85% <ø> (ø)
      @tanstack/eslint-plugin-query∅ <ø> (∅)
      @tanstack/query-async-storage-persister43.85% <ø> (ø)
      @tanstack/query-broadcast-client-experimental24.39% <ø> (ø)
      @tanstack/query-codemods∅ <ø> (∅)
      @tanstack/query-core97.88% <100.00%> (+0.50%):arrow_up:
      @tanstack/query-devtools3.38% <ø> (ø)
      @tanstack/query-persist-client-core80.00% <ø> (ø)
      @tanstack/query-sync-storage-persister84.61% <ø> (ø)
      @tanstack/query-test-utils∅ <ø> (∅)
      @tanstack/react-query96.73% <ø> (ø)
      @tanstack/react-query-devtools9.25% <ø> (ø)
      @tanstack/react-query-next-experimental∅ <ø> (∅)
      @tanstack/react-query-persist-client100.00% <ø> (ø)
      @tanstack/solid-query77.81% <ø> (ø)
      @tanstack/solid-query-devtools64.17% <ø> (ø)
      @tanstack/solid-query-persist-client100.00% <ø> (ø)
      @tanstack/svelte-query∅ <ø> (∅)
      @tanstack/svelte-query-devtools∅ <ø> (∅)
      @tanstack/svelte-query-persist-client∅ <ø> (∅)
      @tanstack/vue-query71.91% <ø> (ø)
      @tanstack/vue-query-devtools∅ <ø> (∅)
      :rocket: New features to boost your workflow:
      • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
      • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
  • TkDodo approved these changes

  • TkDodo merged this53fc74e

  • TkDodo closed this

  • 🎉 This PR has been released!

    Thank you for your contribution!

  • Sign in to join this conversation.