Skip to content
Source
Sign inSign up
TanStack/query

fix(query-core): keep a placeholder that select maps to undefined #12575

Mergedmooxl(GitHub profile, opens in a new tab)merged 2 commits intomain fromrefs/pull/12575/headmerged by TkDodo View on GitHub

🎯 Changes

Fixes #12574

If the previous result was a placeholder, createResult reuses prevResult.data, which select has already run on. When select returned undefined, the result dropped back to pending, and the next call built the placeholder again. Since #12230, useQuery calls getOptimisticResult and setOptions on every render, so it kept flipping between the two.

Now a memoized placeholder stays a placeholder no matter what select returned, the same as on the non-memoized path.

I added one QueryObserver test and one useQuery test. Both fail on main without the fix.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with pnpm run test:pr, or these tests do not apply to this pull request.
  • I have followed the AI contribution policy and fully understand the code in this pull request, including any code generated with AI assistance.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Bug Fixes
    • Fixed query results using keepPreviousData when a selector returns undefined. Reused placeholder results now remain successful and marked as placeholder data, even though the selected data is undefined.
    • Fixed placeholder handling when a query is disabled after its key changes. Reused previous data retains its placeholder status, while a newly computed placeholder that returns undefined remains pending. This keeps loading and disabled query states consistent when previous data is retained.
    1. Pushed9ca43a0

    2. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit 9ca43a0a7416fddd01fb99ba1d6cba92f467f141

      CommandStatusDurationResult
      nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded10m 29sView ↗
      nx run-many --target=build --exclude=examples/*...✅ Succeeded35sView ↗

      ☁️ Nx Cloud last updated this comment at 2026-11-20 21:46:33 UTC

    3. TkDodo approved these changes

    4. More templates

      @tanstack/angular-query

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

      @tanstack/angular-query-devtools

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

      @tanstack/angular-query-persist-client

      npm i https://pkg.pr.new/@tanstack/angular-query-persist-client@12575

      @tanstack/eslint-plugin-query

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

      @tanstack/lit-query

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

      @tanstack/preact-query

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

      @tanstack/preact-query-devtools

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

      @tanstack/preact-query-persist-client

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

      @tanstack/query-async-storage-persister

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

      @tanstack/query-broadcast-client-experimental

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

      @tanstack/query-core

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

      @tanstack/query-devtools

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

      @tanstack/query-persist-client-core

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

      @tanstack/query-sync-storage-persister

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

      @tanstack/react-query

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

      @tanstack/react-query-devtools

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

      @tanstack/react-query-next-experimental

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

      @tanstack/react-query-persist-client

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

      @tanstack/solid-query

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

      @tanstack/solid-query-devtools

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

      @tanstack/solid-query-persist-client

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

      @tanstack/svelte-query

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

      @tanstack/svelte-query-devtools

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

      @tanstack/svelte-query-persist-client

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

      @tanstack/vue-query

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

      @tanstack/vue-query-devtools

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

      commit: 9ca43a0

    5. TkDodo merged this6b849cf

    6. TkDodo closed this

Sign in to join this conversation.