Skip to content

fix(react): do not go into optimistic fetching state when not subscribed#10759

Merged
TkDodo merged 2 commits into
mainfrom
feature/fix/not-subscribed-isFetching
May 23, 2026
Merged

fix(react): do not go into optimistic fetching state when not subscribed#10759
TkDodo merged 2 commits into
mainfrom
feature/fix/not-subscribed-isFetching

Conversation

@TkDodo
Copy link
Copy Markdown
Collaborator

@TkDodo TkDodo commented May 22, 2026

fixes #10718

Summary by CodeRabbit

  • Bug Fixes

    • Prevented the UI from showing optimistic fetching state when a query is not actively subscribed, improving accuracy during subscription toggles.
    • Ensured observer counts are correctly cleared after unsubscribe.
  • Tests

    • Added tests validating subscription toggle behavior, observer cleanup, and UI state consistency across subscription state changes.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 22, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 69712116-0ca5-4b48-b9e1-52f2ee00304b

📥 Commits

Reviewing files that changed from the base of the PR and between 9b318fb and 461fb08.

📒 Files selected for processing (1)
  • .changeset/strong-toes-knock.md
✅ Files skipped from review due to trivial changes (1)
  • .changeset/strong-toes-knock.md

📝 Walkthrough

Walkthrough

This PR prevents optimistic fetching state when a query is unsubscribed by deriving a subscribed flag in useBaseQuery, using it to control optimistic results and subscription logic, adds a test validating toggling subscribed to false, and adds a changeset for a patch release.

Changes

Subscribed State Behavior Fix

Layer / File(s) Summary
Conditional optimistic results based on subscription state
packages/react-query/src/useBaseQuery.ts
useBaseQuery derives subscribed and sets defaultedOptions._optimisticResults to 'optimistic' only when subscribed; when subscribed=false, _optimisticResults becomes undefined. shouldSubscribe now references the derived subscribed value.
Test validation for unsubscribed query state
packages/react-query/src/__tests__/useQuery.test.tsx
New test toggles subscribed from true to false, asserts the queryFn is not called again, verifies the cache observer list becomes empty, and confirms isFetching remains false and fetchStatus remains idle.
Release changeset
.changeset/strong-toes-knock.md
Adds a changeset entry for a patch release describing the fix to avoid optimistic fetching state when the query is not subscribed.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 I nudged a flag, I cleared the mist,

No phantom fetch where none exist.
Subscribed false, the state stays still,
Quiet hooks, no spurious thrill.
Hops of joy — a tiny fix, fulfilled.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description is minimal, containing only a reference to issue #10718 without providing any actual explanation of the changes made. Expand the description to include the '🎯 Changes' section explaining what was fixed and the '✅ Checklist' and '🚀 Release Impact' sections as per the template.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main fix: preventing optimistic fetching state when the query is not subscribed, which directly addresses the reported bug.
Linked Issues check ✅ Passed The code changes successfully address the reported issue: useBaseQuery now respects the subscribed flag when setting optimistic results, preventing isFetching from being true when subscribed is false.
Out of Scope Changes check ✅ Passed All changes are directly relevant to fixing the reported issue; the test validates the fix, useBaseQuery implements it, and the changeset documents the release impact.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/fix/not-subscribed-isFetching

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

@nx-cloud
Copy link
Copy Markdown

nx-cloud Bot commented May 22, 2026

View your CI Pipeline Execution ↗ for commit 9b318fb

Command Status Duration Result
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 1m 56s View ↗
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 1m 23s View ↗

☁️ Nx Cloud last updated this comment at 2026-05-22 22:10:36 UTC

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 22, 2026

🚀 Changeset Version Preview

1 package(s) bumped directly, 24 bumped as dependents.

🟩 Patch bumps

Package Version Reason
@tanstack/react-query 5.100.13 → 5.100.14 Changeset
@tanstack/angular-query-experimental 5.100.13 → 5.100.14 Dependent
@tanstack/angular-query-persist-client 5.100.13 → 5.100.14 Dependent
@tanstack/eslint-plugin-query 5.100.13 → 5.100.14 Dependent
@tanstack/lit-query 0.2.5 → 0.2.6 Dependent
@tanstack/preact-query 5.100.13 → 5.100.14 Dependent
@tanstack/preact-query-devtools 5.100.13 → 5.100.14 Dependent
@tanstack/preact-query-persist-client 5.100.13 → 5.100.14 Dependent
@tanstack/query-async-storage-persister 5.100.13 → 5.100.14 Dependent
@tanstack/query-broadcast-client-experimental 5.100.13 → 5.100.14 Dependent
@tanstack/query-core 5.100.13 → 5.100.14 Dependent
@tanstack/query-devtools 5.100.13 → 5.100.14 Dependent
@tanstack/query-persist-client-core 5.100.13 → 5.100.14 Dependent
@tanstack/query-sync-storage-persister 5.100.13 → 5.100.14 Dependent
@tanstack/react-query-devtools 5.100.13 → 5.100.14 Dependent
@tanstack/react-query-next-experimental 5.100.13 → 5.100.14 Dependent
@tanstack/react-query-persist-client 5.100.13 → 5.100.14 Dependent
@tanstack/solid-query 5.100.13 → 5.100.14 Dependent
@tanstack/solid-query-devtools 5.100.13 → 5.100.14 Dependent
@tanstack/solid-query-persist-client 5.100.13 → 5.100.14 Dependent
@tanstack/svelte-query 6.1.32 → 6.1.33 Dependent
@tanstack/svelte-query-devtools 6.1.32 → 6.1.33 Dependent
@tanstack/svelte-query-persist-client 6.1.32 → 6.1.33 Dependent
@tanstack/vue-query 5.100.13 → 5.100.14 Dependent
@tanstack/vue-query-devtools 6.1.32 → 6.1.33 Dependent

@TkDodo TkDodo marked this pull request as ready for review May 22, 2026 22:04
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 22, 2026

More templates

@tanstack/angular-query-experimental

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

@tanstack/eslint-plugin-query

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

@tanstack/lit-query

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

@tanstack/preact-query

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

@tanstack/preact-query-devtools

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

@tanstack/preact-query-persist-client

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

@tanstack/query-async-storage-persister

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

@tanstack/query-broadcast-client-experimental

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

@tanstack/query-core

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

@tanstack/query-devtools

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

@tanstack/query-persist-client-core

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

@tanstack/query-sync-storage-persister

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

@tanstack/react-query

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

@tanstack/react-query-devtools

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

@tanstack/react-query-next-experimental

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

@tanstack/react-query-persist-client

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

@tanstack/solid-query

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

@tanstack/solid-query-devtools

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

@tanstack/solid-query-persist-client

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

@tanstack/svelte-query

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

@tanstack/svelte-query-devtools

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

@tanstack/svelte-query-persist-client

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

@tanstack/vue-query

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

@tanstack/vue-query-devtools

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

commit: 461fb08

@github-actions
Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size
react full 12.11 KB (+0.05% 🔺)
react minimal 9.08 KB (+0.1% 🔺)

@TkDodo TkDodo merged commit ed20b6d into main May 23, 2026
10 checks passed
@TkDodo TkDodo deleted the feature/fix/not-subscribed-isFetching branch May 23, 2026 15:34
@github-actions github-actions Bot mentioned this pull request May 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

isFetching becomes true when subscribed=false even though no fetch is running

1 participant