fix(preact-query): do not go into optimistic fetching state when not subscribed by giaBaoJS · Pull Request #11259 · TanStack/query · GitHub
Skip to content

fix(preact-query): do not go into optimistic fetching state when not subscribed - #11259

Open
giaBaoJS wants to merge 1 commit into
TanStack:mainfrom
giaBaoJS:fix/preact-unsubscribed-optimistic-fetching
Open

fix(preact-query): do not go into optimistic fetching state when not subscribed#11259
giaBaoJS wants to merge 1 commit into
TanStack:mainfrom
giaBaoJS:fix/preact-unsubscribed-optimistic-fetching

Conversation

@giaBaoJS

@giaBaoJS giaBaoJS commented Aug 23, 2026

Copy link
Copy Markdown

Problem

subscribed: false is broken in @tanstack/preact-query. A hook that opts out of subscribing still reports isFetching: true / fetchStatus: 'fetching' on first render, even though no observer is ever attached and the queryFn is never called.

The Preact adapter was forked from React in #9935 (Feb 2026) and then missed two fixes that landed on the React side afterwards:

Both hooks in preact-query still set _optimisticResults = 'optimistic' unconditionally. In createResult, that branch computes fetchOnMount = !mounted && shouldFetchOnMount(query, options); with no listeners attached, mounted is false and the result is optimistically flipped into fetchState — a fetch that will never happen. useQueries additionally left subscribed out of the useMemo dependency array, so toggling it did not recompute the defaulted options.

Fix

Port the two upstream changes verbatim: derive subscribed once, use it to gate _optimisticResults (undefined when not subscribed), add it to the useQueries dependency array, and reuse it for shouldSubscribe. The diff is identical in shape to react-query's current useBaseQuery.ts / useQueries.ts.

Tests

Two regression tests, mirroring the ones added upstream:

Both are synchronous, so there is no timer race.

Validation

Run from the repo root with pnpm nx run @tanstack/preact-query:test:lib --skip-nx-cache.

  • Baseline on main: 34 files / 524 tests passing, no type errors.
  • With this PR: 34 files / 526 tests passing, no type errors.
  • Red/green, each half checked separately: reverting only useBaseQuery.ts fails just the useQuery test; reverting only useQueries.ts fails just the useQueries test. Reverting both fails both, with isFetching: true / fetchStatus: fetching rendered where false / idle is expected.
  • Also ran test:types, test:eslint, test:build and build for the package, plus prettier --check on the touched files.

Summary by CodeRabbit

  • Bug Fixes

    • Prevented queries from entering a fetching state when subscription is disabled.
    • Disabled query execution and observer registration for unsubscribed queries.
    • Unsubscribed queries now correctly report isFetching: false and an idle fetch status.
  • Tests

    • Added coverage for unsubscribed behavior in useQuery and useQueries.

@coderabbitai

coderabbitai Bot commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

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.

1 participant