Skip to content
Source
Sign inSign up
TanStack/query

fix({react,preact}-query): set the observer options before subscribing #12230

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

🎯 Changes

Fixes #12227.

When the query key changes inside a hidden <Activity>, the effects only run again on reveal, in declaration order. The subscription came first, so onSubscribe fetched the observer's old query before setOptions moved it to the new key. I moved the setOptions effect (and setQueries in useQueries) above useSyncExternalStore. setOptions doesn't fetch while there are no listeners, so the subscription now starts on the current key. The same thing happened when subscribed or isRestoring flipped in the same render as a key change, which is how it shows up in Preact, so I changed it there too.

The PersistQueryClientProvider tests now see one render fewer when restoring finishes. The dropped render was an exact copy of the one before it.

✅ 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 have followed the AI contribution policy and fully understand the code in this pull request, including any code generated with AI assistance.

🚀 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
    • Fixed hidden React <Activity> trees so revealing them fetches and displays data for the current query key without refetching the previous key.
    • Fixed query behavior when keys change as subscriptions are enabled, so only the current key is fetched.
    • Updated single- and multiple-query behavior to use the latest query settings when subscriptions begin, preventing stale results during transitions.
    • Reduced duplicate query-state observations during persistence-provider restoration.
    1. changeset-bot[bot](GitHub profile, opens in a new tab)commented

      🦋 Changeset detected

      Latest commit: 1c72fa8d0632854b844bd4ad270d5d1cbab9ab9c

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

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

      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
      Review in Change Stack →

      Caution

      Review failed

      The pull request is closed.

      No actionable comments were generated in the recent review.

      ℹ️ Recent review info
      ⚙️ Run configuration
      • Configuration used: Repository: TanStack/query/.coderabbit.yaml
      • Review profile: CHILL
      • Plan: Advanced
      • Run ID: c734359b-a7c8-4579-945c-a8332a0d429f
      📥 Commits

      Reviewing files that changed from the base of the PR and between e328fbada6660493c33a5f5437006a5516b47db6 and 9b48c512911bdbe9383806857e39e85acdd21876.

      📒 Files selected for processing: 4
      • .changeset/activity-reveal-current-key.md
      • packages/preact-query/src/__tests__/useQuery.test.tsx
      • packages/preact-query/src/useBaseQuery.ts
      • packages/preact-query/src/useQueries.ts
      🚧 Files skipped from review as they are similar to previous changes: 1
      • .changeset/activity-reveal-current-key.md

      Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

      Walkthrough

      The hooks now apply current observer options before subscribing. Tests cover hidden React Activity query-key changes and Preact subscription changes. Persistence-provider expectations and patch release notes also changed.

      Changes: Activity query-key updates

      Layer / File(s)Summary
      Apply observer options before subscription
      packages/react-query/src/useBaseQuery.ts, packages/react-query/src/useQueries.ts, packages/preact-query/src/useBaseQuery.ts, packages/preact-query/src/useQueries.ts
      The hooks update observers before useSyncExternalStore establishes its subscription.
      Validate current-key subscription behavior
      packages/react-query/src/__tests__/Activity.test.tsx, packages/react-query/src/__tests__/useQuery.test.tsx, packages/react-query/src/__tests__/useQueries.test.tsx, packages/preact-query/src/__tests__/useQuery.test.tsx
      Tests check that revealing a hidden React Activity tree fetches the new key without refetching the old key. The Preact test checks a key change when subscription is enabled.
      Update persistence expectations and release notes
      packages/react-query-persist-client/src/__tests__/PersistQueryClientProvider.test.tsx, .changeset/activity-reveal-current-key.md
      Persistence tests reduce the expected observation counts and remove duplicate state assertions. The changeset records patch releases for React Query and Preact Query.

      Estimated code review effort: 3 (Moderate) | ~20 minutes

      Change: Bug fix · Severity of issue fixed: Medium

      Possibly related PRs

      • TanStack/query#12293: The new tests use the createKey test helper introduced by this PR, and the PRs overlap in test files.

      Suggested reviewers: tkdodo, sukvvon

      Merge Risk: Minimal · up to 1c72f

      The hooks apply current options before subscribing. No material merge risk was identified.

      Pre-merge checks: Passed 4 · Failed 1
      Failed Failed checks: 1 warning
      Check nameStatusExplanationResolution
      Docstring CoverageWarningDocstring coverage is 57.14% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 8 files. (1 skipped: 1…Write docstrings for the functions missing them to satisfy the coverage threshold.
      Passed Passed checks: 4 passed
      Check nameStatusExplanation
      Title checkPassedThe title clearly and concisely describes the primary change: updating observer options before subscribing in React and Preact Query.
      Description checkPassedThe description explains the bug, implementation, affected behavior, test impact, completed checklist items, and release impact. It includes a changeset and local test validation.
      Linked Issues checkPassedIssue #12227 requires the observer to use the current query key before reveal-time subscription. useBaseQuery now runs observer.setOptions before useSyncExternalStore. useQueries now runs `obs…
      Out of Scope Changes checkPassedThe production changes support the same observer-ordering fix. The React tests cover the linked Activity case. The Preact changes cover the analogous same-render subscription state change described in…
      Full details: Linked Issues check

      Explanation

      Issue #12227 requires the observer to use the current query key before reveal-time subscription. useBaseQuery now runs observer.setOptions before useSyncExternalStore. useQueries now runs observer.setQueries before its subscription. The new React useQuery and useQueries tests verify one fetch for the original key and one fetch for the new key.

      Full details: Out of Scope Changes check

      Explanation

      The production changes support the same observer-ordering fix. The React tests cover the linked Activity case. The Preact changes cover the analogous same-render subscription state change described in the current PR intent. Persistence expectation updates and the changeset document behavior caused by the fix. No unrelated change is demonstrated.

      Full details: Docstring Coverage

      Explanation

      Docstring coverage is 57.14% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 8 files. (1 skipped: 1 unsupported.)

      Resolution

      Write docstrings for the functions missing them to satisfy the coverage threshold.

      • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

      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
    3. lazerg(GitHub profile, opens in a new tab) changed the title

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

      View your CI Pipeline Execution ↗ for commit 1c72fa8d0632854b844bd4ad270d5d1cbab9ab9c

      CommandStatusDurationResult
      nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded3m 55sView ↗
      nx run-many --target=build --exclude=examples/*...✅ Succeeded1sView ↗

      ☁️ Nx Cloud last updated this comment at 2026-11-01 11:09:08 UTC

    5. 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@12230

      @tanstack/eslint-plugin-query

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

      @tanstack/lit-query

      npm i https://pkg.pr.new/@tanstack/lit-query@12230

      @tanstack/preact-query

      npm i https://pkg.pr.new/@tanstack/preact-query@12230

      @tanstack/preact-query-devtools

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

      @tanstack/preact-query-persist-client

      npm i https://pkg.pr.new/@tanstack/preact-query-persist-client@12230

      @tanstack/query-async-storage-persister

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

      @tanstack/query-broadcast-client-experimental

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

      @tanstack/query-core

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

      @tanstack/query-devtools

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

      @tanstack/query-persist-client-core

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

      @tanstack/query-sync-storage-persister

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

      @tanstack/react-query

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

      @tanstack/react-query-devtools

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

      @tanstack/react-query-next-experimental

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

      @tanstack/react-query-persist-client

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

      @tanstack/solid-query

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

      @tanstack/solid-query-devtools

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

      @tanstack/solid-query-persist-client

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

      @tanstack/svelte-query

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

      @tanstack/svelte-query-devtools

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

      @tanstack/svelte-query-persist-client

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

      @tanstack/vue-query

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

      @tanstack/vue-query-devtools

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

      commit: 1c72fa8

    6. TkDodo merged this1e1caa5

    7. TkDodo closed this

Sign in to join this conversation.