fix(react-query): do not report isFetching when subscribed is false#10739
fix(react-query): do not report isFetching when subscribed is false#10739LeSingh1 wants to merge 1 commit into
Conversation
When useQuery or useQueries is called with subscribed: false, the observer never subscribes to the query, so no fetch is ever triggered. Previously the optimistic result still advertised fetchStatus 'fetching', causing isFetching to be true while the cache state remained idle. The optimistic fetching state is now skipped in that case so the returned result matches the underlying query state. Fixes TanStack#10718
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
📝 WalkthroughWalkthroughThis PR fixes an inconsistency where ChangesFix inconsistent fetching state for non-subscribed observers
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Fixes #10718.
When
useQueryoruseQueriesis called withsubscribed: false, the observer never subscribes to the query and no fetch is triggered. However,useBaseQueryanduseQuerieswere unconditionally setting_optimisticResultsto'optimistic', which makesQueryObserver.createResultflipfetchStatusto'fetching'whenevershouldFetchOnMountreturns true. The result was an inconsistent state where the returned object reportedisFetching: trueandfetchStatus: 'fetching'while the underlying query stayed atfetchStatus: 'idle'andqueryFnwas never called. This is exactly the pattern documented for React Native, where consumers togglesubscribedto pause queries on unfocused screens, so the inconsistency was easy to hit and confusing to debug.The fix is small. In both
useBaseQueryanduseQueries, whenoptions.subscribed === false, we skip the optimistic fetching state and leave_optimisticResultsundefined so that the observer returns the actual cache state. TheisRestoringbranch is unchanged. Oncesubscribedflips back to true on a later render, the optimistic state kicks back in as before, so re-subscribe behavior is unaffected.A regression test has been added under the existing
subscribeddescribe block inuseQuery.test.tsx. It renders a query withsubscribed: false, checks that every recorded state hasisFetching: falseandfetchStatus: 'idle', and asserts thatqueryFnis never called. I confirmed the test fails onmainand passes with the fix. The full@tanstack/react-queryand@tanstack/query-corevitest suites pass locally, type checks are clean, and a patch changeset is included.Summary by CodeRabbit
Release Notes
Bug Fixes
Tests