fix({react,preact}-query): set the observer options before subscribing #12230
main fromrefs/pull/12230/head·merged 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.
- Fixed hidden React
- 1
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.mdpackages/preact-query/src/__tests__/useQuery.test.tsxpackages/preact-query/src/useBaseQuery.tspackages/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.tsThe hooks update observers before useSyncExternalStoreestablishes 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.tsxTests 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.mdPersistence 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
createKeytest helper introduced by this PR, and the PRs overlap in test files.
Suggested reviewers:
tkdodo,sukvvon - TanStack/query#12293: The new tests use the
Merge Risk:
Minimal · up to1c72f-
The hooks apply current options before subscribing. No material merge risk was identified.
Pre-merge checks:
4 · 1-
Failed checks: 1 warningCheck name Status Explanation Resolution Docstring Coverage 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… Write docstrings for the functions missing them to satisfy the coverage threshold. Passed checks: 4 passed-
Check name Status Explanation Title check The title clearly and concisely describes the primary change: updating observer options before subscribing in React and Preact Query. Description check The 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 check Issue #12227requires the observer to use the current query key before reveal-time subscription.useBaseQuerynow runsobserver.setOptionsbeforeuseSyncExternalStore.useQueriesnow runs `obs…Out of Scope Changes check 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… Full details: Linked Issues check
-
Explanation
Issue
#12227requires the observer to use the current query key before reveal-time subscription.useBaseQuerynow runsobserver.setOptionsbeforeuseSyncExternalStore.useQueriesnow runsobserver.setQueriesbefore its subscription. The new ReactuseQueryanduseQueriestests 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.
-
lazerg(GitHub profile, opens in a new tab) changed the title
View your CI Pipeline Execution ↗ for commit 1c72fa8d0632854b844bd4ad270d5d1cbab9ab9c
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 3m 55s View ↗ nx run-many --target=build --exclude=examples/*...✅ Succeeded 1s View ↗
☁️ Nx Cloud last updated this comment at
2026-11-01 11:09:08UTCMore templates
- @tanstack/query-example-angular-auto-refetching
- @tanstack/query-example-angular-basic
- @tanstack/query-example-angular-basic-persister
- @tanstack/query-example-angular-devtools-panel-angular-router
- @tanstack/query-example-angular-infinite-query-with-max-pages
- @tanstack/query-example-angular-optimistic-updates
- @tanstack/query-example-angular-pagination
- @tanstack/query-example-angular-query-options-from-a-service-angular-router
- @tanstack/query-example-angular-router
- @tanstack/query-example-angular-rxjs
- @tanstack/query-example-angular-simple
- @tanstack/query-example-lit-algolia
- @tanstack/query-example-lit-auto-refetching
- @tanstack/query-example-lit-basic
- @tanstack/query-example-lit-basic-graphql-request
- @tanstack/query-example-lit-batching
- @tanstack/query-example-lit-chat
- @tanstack/query-example-lit-default-query-function
- @tanstack/query-example-lit-infinite-query-with-max-pages
- @tanstack/query-example-lit-load-more-infinite-scroll-lit-labs-router
- @tanstack/query-example-lit-offline-lit-labs-router
- @tanstack/query-example-lit-optimistic-updates
- @tanstack/query-example-lit-pagination
- @tanstack/query-example-lit-playground
- @tanstack/query-example-lit-prefetching-lit-labs-router
- @tanstack/query-example-lit-rick-morty-lit-labs-router
- @tanstack/query-example-lit-simple
- @tanstack/query-example-lit-ssr
- @tanstack/query-example-lit-star-wars-lit-labs-router
- @tanstack/query-example-preact-algolia
- @tanstack/query-example-preact-auto-refetching
- @tanstack/query-example-preact-basic
- @tanstack/query-example-preact-basic-graphql-request
- @tanstack/query-example-preact-batching
- @tanstack/query-example-preact-chat
- @tanstack/query-example-preact-default-query-function
- @tanstack/query-example-preact-devtools-panel
- @tanstack/query-example-preact-infinite-query-with-max-pages
- @tanstack/query-example-preact-load-more-infinite-scroll-preact-iso
- @tanstack/query-example-preact-offline-preact-iso
- @tanstack/query-example-preact-optimistic-updates
- @tanstack/query-example-preact-pagination
- @tanstack/query-example-preact-playground
- @tanstack/query-example-preact-prefetching-preact-iso
- @tanstack/query-example-preact-rick-morty-preact-iso
- @tanstack/query-example-preact-shadow-dom
- @tanstack/query-example-preact-simple
- @tanstack/query-example-preact-ssr
- @tanstack/query-example-preact-ssr-streaming
- @tanstack/query-example-preact-star-wars-preact-iso
- @tanstack/query-example-preact-suspense
- @tanstack/query-example-react-algolia
- @tanstack/query-example-react-auto-refetching-nextjs-pages-router
- @tanstack/query-example-react-basic
- @tanstack/query-example-react-basic-graphql-request
- @tanstack/query-example-react-batching
- @tanstack/query-example-react-chat
- @tanstack/query-example-react-default-query-function
- @tanstack/query-example-react-devtools-panel
- @tanstack/query-example-react-eslint-legacy
- @tanstack/query-example-react-eslint-plugin-demo
- @tanstack/query-example-react-infinite-query-with-max-pages-nextjs-pages-router
- @tanstack/query-example-react-load-more-infinite-scroll-nextjs-pages-router
- @tanstack/query-example-react-nextjs
- @tanstack/query-example-react-nextjs-app-optimistic-updates
- @tanstack/query-example-react-nextjs-app-prefetching
- @tanstack/query-example-react-nextjs-suspense-streaming
- @tanstack/query-example-react-offline
- @tanstack/query-example-react-pagination-nextjs-pages-router
- @tanstack/query-example-react-playground
- @tanstack/query-example-react-prefetching-nextjs-pages-router
- @tanstack/query-example-react-react-native
- @tanstack/query-example-react-react-router
- @tanstack/query-example-react-rick-morty-react-router
- @tanstack/query-example-react-shadow-dom
- @tanstack/query-example-react-simple
- @tanstack/query-example-react-star-wars-react-router
- @tanstack/query-example-react-suspense
- @tanstack/query-example-solid-algolia
- @tanstack/query-example-solid-astro
- @tanstack/query-example-solid-auto-refetching-solid-start2
- @tanstack/query-example-solid-basic
- @tanstack/query-example-solid-basic-graphql-request
- @tanstack/query-example-solid-batching
- @tanstack/query-example-solid-chat
- @tanstack/query-example-solid-default-query-function
- @tanstack/query-example-solid-devtools-panel
- @tanstack/query-example-solid-offline
- @tanstack/query-example-solid-optimistic-updates-solid-start2
- @tanstack/query-example-solid-pagination-solid-start2
- @tanstack/query-example-solid-playground
- @tanstack/query-example-solid-prefetching-solid-start2
- @tanstack/query-example-solid-rick-morty-solid-router
- @tanstack/query-example-solid-shadow-dom
- @tanstack/query-example-solid-simple
- @tanstack/query-example-solid-solid-router
- @tanstack/query-example-solid-solid-start-streaming
- @tanstack/query-example-solid-star-wars-solid-router
- @tanstack/query-example-solid-suspense
- @tanstack/query-example-svelte-algolia
- @tanstack/query-example-svelte-auto-refetching-sveltekit
- @tanstack/query-example-svelte-basic-graphql-request
- @tanstack/query-example-svelte-basic-sveltekit
- @tanstack/query-example-svelte-batching
- @tanstack/query-example-svelte-chat
- @tanstack/query-example-svelte-default-query-function
- @tanstack/query-example-svelte-infinite-query-with-max-pages-sveltekit
- @tanstack/query-example-svelte-load-more-infinite-scroll-sveltekit
- @tanstack/query-example-svelte-offline-sveltekit
- @tanstack/query-example-svelte-optimistic-updates-sveltekit
- @tanstack/query-example-svelte-pagination-sveltekit
- @tanstack/query-example-svelte-playground-sveltekit
- @tanstack/query-example-svelte-prefetching-sveltekit
- @tanstack/query-example-svelte-rick-morty-sveltekit
- @tanstack/query-example-svelte-shadow-dom
- @tanstack/query-example-svelte-simple
- @tanstack/query-example-svelte-ssr-sveltekit
- @tanstack/query-example-svelte-star-wars-sveltekit
- @tanstack/query-example-svelte-sveltekit
- @tanstack/query-example-vue-2.6-basic
- @tanstack/query-example-vue-2.7-basic
- @tanstack/query-example-vue-algolia
- @tanstack/query-example-vue-auto-refetching-nuxt4
- @tanstack/query-example-vue-basic
- @tanstack/query-example-vue-basic-graphql-request
- @tanstack/query-example-vue-batching
- @tanstack/query-example-vue-chat
- @tanstack/query-example-vue-default-query-function
- @tanstack/query-example-vue-dependent-queries
- @tanstack/query-example-vue-devtools-panel
- @tanstack/query-example-vue-infinite-query-with-max-pages-nuxt4
- @tanstack/query-example-vue-load-more-infinite-scroll-nuxt4
- @tanstack/query-example-vue-offline-vue-router
- @tanstack/query-example-vue-optimistic-updates-nuxt4
- @tanstack/query-example-vue-pagination-nuxt4
- @tanstack/query-example-vue-persister
- @tanstack/query-example-vue-playground
- @tanstack/query-example-vue-prefetching-nuxt4
- @tanstack/query-example-vue-rick-morty-vue-router
- @tanstack/query-example-vue-shadow-dom
- @tanstack/query-example-vue-simple
- @tanstack/query-example-vue-simple-nuxt3
- @tanstack/query-example-vue-simple-nuxt4
- @tanstack/query-example-vue-star-wars-vue-router
- @tanstack/query-example-vue-suspense
- @tanstack/query-example-vue-vue-router
@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@12230commit:
1c72fa8TkDodo merged this1e1caa5
TkDodo closed this
Sign in to join this conversation.
🦋 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
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