Skip to content
Source
Sign inSign up
TanStack/query

fix(react): allow using `enabled` when using `useQuery().promise` #8501

ClosedKATT(GitHub profile, opens in a new tab)wants to merge 15 commits intomain fromrefs/pull/8501/headopened View on GitHub

    1. github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: query-core

    2. View your CI Pipeline Execution ↗ for commit 5e9f958140447148378e3d64ce1bd0487392847a.

      CommandStatusDurationResult
      nx affected --targets=test:sherif,test:knip,tes...❌ Failed2m 57sView ↗
      nx run-many --target=build --exclude=examples/*...❌ Failed1m 47sView ↗

      ☁️ Nx Cloud last updated this comment at 2025-05-04 10:17:08 UTC

    3. Open in Stackblitz

      More templates

      @tanstack/angular-query-devtools-experimental

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

      @tanstack/eslint-plugin-query

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

      @tanstack/angular-query-experimental

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

      @tanstack/query-async-storage-persister

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

      @tanstack/query-core

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

      @tanstack/query-broadcast-client-experimental

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

      @tanstack/query-devtools

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

      @tanstack/query-persist-client-core

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

      @tanstack/query-sync-storage-persister

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

      @tanstack/react-query

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

      @tanstack/react-query-devtools

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

      @tanstack/react-query-next-experimental

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

      @tanstack/react-query-persist-client

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

      @tanstack/solid-query

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

      @tanstack/solid-query-devtools

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

      @tanstack/solid-query-persist-client

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

      @tanstack/svelte-query

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

      @tanstack/svelte-query-devtools

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

      @tanstack/svelte-query-persist-client

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

      @tanstack/vue-query

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

      @tanstack/vue-query-devtools

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

      commit: 2f69183

    4. github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: react-query

    5. KATT(GitHub profile, opens in a new tab) changed the title

    6. packages/query-core/src/__tests__/queryObserver.test.tsx lines 1280 to 1290
      @@ -1233,3 +1234,18 @@
      12331234
      12341235 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)

      Maybe it should just have a rejected promise when enabled is false instead?

      TkDodocommented

      agree that’s how it should be 👍

    7. Codecov Report

      All modified and coverable lines are covered by tests :white_check_mark:

      Project coverage is 63.04%. Comparing base (6184a89) to head (2f69183). Report is 9 commits behind head on main.

      Additional details and impacted files

      Impacted file tree graph

      @@             Coverage Diff             @@
      ##             main    #8501       +/-   ##
      ===========================================
      + Coverage   46.25%   63.04%   +16.78%     
      ===========================================
        Files         199      135       -64     
        Lines        7530     4835     -2695     
        Branches     1719     1358      -361     
      ===========================================
      - Hits         3483     3048      -435     
      + Misses       3668     1543     -2125     
      + Partials      379      244      -135     
      ComponentsCoverage Δ
      @tanstack/angular-query-devtools-experimental∅ <ø> (∅)
      @tanstack/angular-query-experimental88.65% <ø> (ø)
      @tanstack/eslint-plugin-query∅ <ø> (∅)
      @tanstack/query-async-storage-persister43.85% <ø> (ø)
      @tanstack/query-broadcast-client-experimental∅ <ø> (∅)
      @tanstack/query-codemods∅ <ø> (∅)
      @tanstack/query-core94.15% <ø> (ø)
      @tanstack/query-devtools4.78% <ø> (ø)
      @tanstack/query-persist-client-core57.73% <ø> (ø)
      @tanstack/query-sync-storage-persister84.61% <ø> (ø)
      @tanstack/react-query95.65% <100.00%> (+0.10%):arrow_up:
      @tanstack/react-query-devtools10.00% <ø> (ø)
      @tanstack/react-query-next-experimental∅ <ø> (∅)
      @tanstack/react-query-persist-client100.00% <ø> (ø)
      @tanstack/solid-query78.20% <ø> (ø)
      @tanstack/solid-query-devtools∅ <ø> (∅)
      @tanstack/solid-query-persist-client100.00% <ø> (ø)
      @tanstack/svelte-query87.33% <ø> (ø)
      @tanstack/svelte-query-devtools∅ <ø> (∅)
      @tanstack/svelte-query-persist-client100.00% <ø> (ø)
      @tanstack/vue-query71.01% <78.57%> (-0.45%):arrow_down:
      @tanstack/vue-query-devtools∅ <ø> (∅)
    8. TkDodo reviewed

      TkDodoreplied on packages/query-core/src/__tests__/queryObserver.test.tsx lines 1280 to 1290

      agree that’s how it should be 👍

      packages/react-query/src/useBaseQuery.ts lines 156 to 160
      @@ -152,4 +150,11 @@
      152150 !isServer &&
      153151 willFetch(result, isRestoring)
      154152 ) {
      155- const promise = isNewCacheEntry
      153+ // This fetching in the render should likely be done as part of the getOptimisticResult() considering https://github.com/TanStack/query/issues/8507
      154+ const state = cacheEntry?.state
      155+
      156+ const shouldFetch =
      157+ !state ||
      158+ (state.data === undefined &&
      159+ state.status === 'pending' &&
      160+ state.fetchStatus === 'idle')
      TkDodocommented

      I would really want this to only trigger if there is no cache entry. As soon as you have a cache entry, no matter what state, the “thing” becomes observable by other components, making doing something “in render” not allowed by the rules of react.

      I guess this was done to catch transitions from enabled:false to enabled:true ?

    9. TkDodo closed this

Sign in to join this conversation.