fix(react): allow using `enabled` when using `useQuery().promise` #8501
ClosedKATT(GitHub profile, opens in a new tab)wants to merge 15 commits into
main fromrefs/pull/8501/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelspackage: query-core
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: react-query
KATT(GitHub profile, opens in a new tab) changed the title
- packages/query-core/src/__tests__/queryObserver.test.tsx lines 1280 to 1290@@ -1233,3 +1234,18 @@1233123412341235 unsubscribe()12351236 })1237+1238+ test('switching enabled state should reuse the same promise', async () => {1239+ const key = queryKey()1240+1241+ const observer = new QueryObserver(queryClient, {1242+ queryKey: key,1243+ enabled: false,1244+ queryFn: () => 'data',1245+ })1246+ const results: Array<QueryObserverResult> = []1247+1248+ const success = pendingThenable<void>()1249+1250+ const unsubscribe = observer.subscribe((result) => {1251+ results.push(result)
TkDodo reviewed
packages/react-query/src/useBaseQuery.ts lines 156 to 160@@ -152,4 +150,11 @@152150 !isServer &&153151 willFetch(result, isRestoring)154152 ) {155- const promise = isNewCacheEntry153+ // This fetching in the render should likely be done as part of the getOptimisticResult() considering https://github.com/TanStack/query/issues/8507154+ const state = cacheEntry?.state155+156+ const shouldFetch =157+ !state ||158+ (state.data === undefined &&159+ state.status === 'pending' &&160+ state.fetchStatus === 'idle')TkDodo closed this
Sign in to join this conversation.
View your CI Pipeline Execution ↗ for commit 5e9f958140447148378e3d64ce1bd0487392847a.
nx affected --targets=test:sherif,test:knip,tes...nx run-many --target=build --exclude=examples/*...☁️ Nx Cloud last updated this comment at
2025-05-04 10:17:08UTC