fix({react,preact}-query): set the observer options before subscribing by lazerg · Pull Request #12230 · TanStack/query · GitHub
Skip to content

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

Open
lazerg wants to merge 2 commits into
TanStack:mainfrom
lazerg:fix/issue-12227-activity-reveal-stale-key
Open

lazerg wants to merge 2 commits into
TanStack:mainfrom
lazerg:fix/issue-12227-activity-reveal-stale-key

Conversation

@lazerg

@lazerg lazerg commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

🎯 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 instead of the previous one.
    • Prevented stale requests when query keys change or subscriptions are enabled during transitions.
    • Updated query and multi-query behavior to apply the latest options before subscriptions begin.
    • Corrected persistence-provider hydration state updates for standard queries, initial-data queries, and multiple-client scenarios.

…aling an 'Activity' does not fetch the previous key
@changeset-bot

changeset-bot Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

@coderabbitai

coderabbitai Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

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 e328fba and 9b48c51.

📒 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. Regression tests cover hidden React Activity query-key changes and Preact subscription changes. Persistence-provider expectations and patch release notes were updated.

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__/useQuery.test.tsx, packages/react-query/src/__tests__/useQueries.test.tsx, packages/preact-query/src/__tests__/useQuery.test.tsx
Tests verify that hidden Activity updates and simultaneous subscription changes fetch the current query key once.
Update persistence expectations and release notes
packages/react-query-persist-client/src/__tests__/PersistQueryClientProvider.test.tsx, .changeset/activity-reveal-current-key.md
Persistence tests expect fewer recorded states. The changeset records patch releases for React Query and Preact Query.

Priority: ➖ Normal

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

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: tkdodo, sukvvon

Merge Risk: ⚪ Minimal · up to 9b48c

No actionable issue remains from the reviewed changes; the PR is ready to merge after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning 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 Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the primary change: updating observer options before subscribing in React and Preact Query.
Description check Passed 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 Passed 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 `obs…
Out of Scope Changes check Passed 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 #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.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • 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

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

@lazerg lazerg changed the title fix(react-query): set the observer options before subscribing so revealing an 'Activity' does not fetch the previous key fix({react,preact}-query): set the observer options before subscribing Oct 21, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

useBaseQuery fetches the old query key when an <Activity> tree is revealed

1 participant