Skip to content
Source
Sign inSign up
TanStack/query

fix(query-core): prevent placeholder render loops 🤖🤖🤖 #12642

Closedsakaintp(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/12642/headopened View on GitHub

🎯 Changes

Fixes #12639.

A parent rerender can recreate placeholderData. The rendered result then differs from the subscription snapshot or selection creates a fresh non-plain value, so React keeps requesting another render.

  • Sync changed optimistic results to QueriesObserver when its observer sequence is unchanged.
  • Structurally share placeholder inputs and reuse a selection only for the same query state, input, and selector when the selected value is non-plain or selection threw.
  • Cover recreated class placeholders, changing plain placeholders, Set selection, and throwing selectors. The five regressions fail on f6689e0 with Maximum update depth exceeded and pass with this patch.

Verification

  • pnpm vitest run --maxWorkers=1: query-core 1640 passed, react-query 625 passed, preact-query 570 passed.
  • pnpm run test:types: TypeScript 5.7, 5.8, 5.9, current, 7.0, 7.1 passed for query-core and react-query.
  • Oxlint passed for both packages; Oxfmt and git diff --check passed.
  • Full monorepo pnpm run test:pr was not run.
  • Preview Size comparison is pending/unavailable until fork workflows run; no size change is claimed.

✅ 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 infinite re-renders with placeholder data when its identity changes or a selector returns a new value or throws.
  • Tests
    • Added regression coverage for placeholder data and selector behavior in queries.
    1. 🦋 Changeset detected

      Latest commit: 944e939d002527cbacdc2bd3231fab90a5e7e4d3

      The changes in this PR will be included in the next version bump.

      This PR includes changesets to release 26 packages
      NameType
      @tanstack/query-corePatch
      @tanstack/react-queryPatch
      @tanstack/angular-queryPatch
      @tanstack/lit-queryPatch
      @tanstack/preact-queryPatch
      @tanstack/query-async-storage-persisterPatch
      @tanstack/query-broadcast-client-experimentalPatch
      @tanstack/query-devtoolsPatch
      @tanstack/query-persist-client-corePatch
      @tanstack/query-sync-storage-persisterPatch
      @tanstack/solid-queryPatch
      @tanstack/svelte-queryPatch
      @tanstack/vue-queryPatch
      @tanstack/react-query-devtoolsPatch
      @tanstack/react-query-next-experimentalPatch
      @tanstack/react-query-persist-clientPatch
      @tanstack/angular-query-devtoolsPatch
      @tanstack/angular-query-persist-clientPatch
      @tanstack/preact-query-devtoolsPatch
      @tanstack/preact-query-persist-clientPatch
      @tanstack/solid-query-devtoolsPatch
      @tanstack/svelte-query-devtoolsPatch
      @tanstack/vue-query-devtoolsPatch
      @tanstack/solid-query-persist-clientPatch
      @tanstack/svelte-query-persist-clientPatch
      @tanstack/eslint-plugin-queryPatch

      Not sure what this means? Click here to learn what changesets are.

      Click here if you're a maintainer who wants to add another changeset to this PR

    2. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Explain Change →

      No actionable comments were generated in the recent review.

      Walkthrough

      The changes update placeholder selection reuse in QueryObserver and optimistic-result caching in QueriesObserver. New useQuery and useQueries tests cover placeholder data that changes identity, selector results, and selector errors.

      Changes: Placeholder render stability

      Layer / File(s)Summary
      Placeholder selection reuse
      packages/query-core/src/queryObserver.ts, packages/react-query/src/__tests__/useQuery.test.tsx
      QueryObserver records selection input and query state. It reuses eligible placeholder selections. Tests cover a selector returning a Set and a throwing selector.
      Optimistic results and multi-query coverage
      packages/query-core/src/queriesObserver.ts, packages/react-query/src/__tests__/useQueries.test.tsx, .changeset/quiet-rows-settle.md
      QueriesObserver synchronizes cached observer matches and results when observer order is unchanged and a result differs shallowly. Tests cover class-instance, changing plain-object, and selected Set placeholders. The changeset declares patch releases for @tanstack/query-core and @tanstack/react-query.

      Estimated code review effort: 3 (Moderate) | ~20 minutes

      Change: Bug fix · Severity of issue fixed: Medium

      Possibly related issues

      • TanStack/query#12567 — The PR changes reuse and comparison behavior for selected placeholder results, which is relevant to this issue’s question about comparing -0 and 0.
      • TanStack/query#12640 — The PR changes reuse of selections when a selector throws for placeholder data; whether it resolves the reported reset case is not established.
      • TanStack/query#12574 — The PR addresses a related render loop by changing placeholder selection reuse, though this issue reports a keepPreviousData case.

      Possibly related PRs

      • TanStack/query#12564 — Both changes modify selected-placeholder reuse and selector identity checks in query-core.
      • TanStack/query#12632 — Both changes affect cached result state in queriesObserver.ts.
      • TanStack/query#11536 — Both changes define selector-identity conditions for placeholder selection reuse.

      Suggested reviewers: sukvvon, tkdodo

      Merge Risk: Minimal · up to 944e9

      This change stops placeholder-data render loops in useQuery and useQueries, and regression tests cover the reported cases. No merge-blocking risk was identified in the reviewed changes.

      Pre-merge checks: Passed 4

      Check nameStatusExplanation
      Title checkPassedThe title names the fix and the area it touches, "prevent placeholder render loops" in query-core, which matches the changeset. It is a conventional-commit style title. The trailing robot emojis are n…
      Description checkPassedThe description follows the repository template. It has the Changes section with a motivation and a summary of the fix, the Checklist, and the Release Impact section. The changeset box is checked, whi…
      Linked Issues checkPassedDirectly linked issue #12639 has coding requirements only. It asks that placeholder results settle when placeholderData or its select result is not referentially stable, for useQuery and useQueries. T…
      Out of Scope Changes checkPassedAll changed files relate to the objective. Changes are limited to query-core queryObserver.ts and queriesObserver.ts, the matching react-query regression tests in useQuery.test.tsx and useQueries.test…
      Full details: Title check

      Explanation

      The title names the fix and the area it touches, "prevent placeholder render loops" in query-core, which matches the changeset. It is a conventional-commit style title. The trailing robot emojis are noise that the title guidance asks to avoid, but the title still describes the main change accurately.

      Full details: Description check

      Explanation

      The description follows the repository template. It has the Changes section with a motivation and a summary of the fix, the Checklist, and the Release Impact section. The changeset box is checked, which matches the changeset file in the PR. The author states that the full monorepo test:pr run was not run, and the test-PR checkbox is left unchecked, which is accurate. The description covers what changed, why, and how it was verified.

      Full details: Linked Issues check

      Explanation

      Directly linked issue #12639 has coding requirements only. It asks that placeholder results settle when placeholderData or its select result is not referentially stable, for useQuery and useQueries. The diff addresses each mechanism in the issue:

      1. queryObserver.ts: the placeholder path now passes #selectInput (the last selected input) to replaceData instead of the previous result data when selection applies. This keeps structural sharing of placeholder inputs.
      2. queryObserver.ts: canReusePlaceholderSelect reuses #selectResult for placeholder data when prevResultState matches #selectInputState and data matches #selectInput. Reuse applies only when the selected value is non-plain (not a plain object or array) or selection stored an error. This covers class instances, Set selections, and throwing selectors.
      3. queriesObserver.ts: getOptimisticResult updates #observerMatches and #result when the observer sequence is unchanged and an optimistic result differs shallowly. This covers useQueries.

      Added tests in useQuery.test.tsx and useQueries.test.tsx cover recreated class placeholders, changing plain placeholders, Set selection, and throwing selectors. The PR reports these fail on f6689e0 and pass with the patch. I did not run the test suite. The changing-plain-data case relies on the replaceData change; its settle behavior rests on the reported test results and on the code reading, not on an execution I performed.

      Full details: Out of Scope Changes check

      Explanation

      All changed files relate to the objective. Changes are limited to query-core queryObserver.ts and queriesObserver.ts, the matching react-query regression tests in useQuery.test.tsx and useQueries.test.tsx, and a patch changeset. The imports of isPlainArray and isPlainObject support the reuse condition. The selectInput and selectInputState fields support the placeholder selection memoization. The queriesObserver change addresses the useQueries loop. No unrelated behavior or public API change appears in the diff. The PR description and summary state that no public declarations changed.

      Finishing Touches

      Generate unit tests (beta)

      • Create a new PR
      • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

      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.

      Share

      Recent review info

      Run configuration

      Commits

      Reviewing files that changed from the base of the PR and between f6689e04be493e2e83f9c93d7e478f92c3e5d328 and 944e939d002527cbacdc2bd3231fab90a5e7e4d3.

      Files selected for processing: 5

      • .changeset/quiet-rows-settle.md
      • packages/query-core/src/queriesObserver.ts
      • packages/query-core/src/queryObserver.ts
      • packages/react-query/src/__tests__/useQueries.test.tsx
      • packages/react-query/src/__tests__/useQuery.test.tsx

      Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

    3. tanstack-source added labelsbug

    4. TkDodo closed this

Sign in to join this conversation.