fix(query-core): prevent placeholder render loops 🤖🤖🤖 #12642
main fromrefs/pull/12642/head·opened 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
QueriesObserverwhen 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,
Setselection, and throwing selectors. The five regressions fail onf6689e0withMaximum update depth exceededand 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 --checkpassed. - Full monorepo
pnpm run test:prwas 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
No actionable comments were generated in the recent review.
Walkthrough
-
The changes update placeholder selection reuse in
QueryObserverand optimistic-result caching inQueriesObserver. NewuseQueryanduseQueriestests 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.tsxQueryObserverrecords selection input and query state. It reuses eligible placeholder selections. Tests cover a selector returning aSetand 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.mdQueriesObserversynchronizes cached observer matches and results when observer order is unchanged and a result differs shallowly. Tests cover class-instance, changing plain-object, and selectedSetplaceholders. The changeset declares patch releases for@tanstack/query-coreand@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
-0and0. - 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
keepPreviousDatacase.
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 - TanStack/query#12567 — The PR changes reuse and comparison behavior for selected placeholder results, which is relevant to this issue’s question about comparing
Merge Risk:
Minimal · up to944e9-
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:
4-
Check name Status Explanation Title check 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 n… Description check 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, whi… Linked Issues check Directly linked issue #12639has 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 check 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… 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
#12639has 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:- 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. - queryObserver.ts: canReusePlaceholderSelect reuses
#selectResultfor placeholder data when prevResultState matches#selectInputStateand 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. - queriesObserver.ts: getOptimisticResult updates
#observerMatchesand#resultwhen 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.
- queryObserver.ts: the placeholder path now passes
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.
Recent review info-
Run configuration-
- Source mode: Auto
- Repository: TanStack/query
- Branch: main
- File: .coderabbit.yaml
- Commit: f6689e0
- Layers: Repository YAML
- Review profile: CHILL
- Plan: Advanced
- Run ID:
86043316-9a7e-44a5-8280-24a668ec073d
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.mdpackages/query-core/src/queriesObserver.tspackages/query-core/src/queryObserver.tspackages/react-query/src/__tests__/useQueries.test.tsxpackages/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.
-
-
sakaintp(GitHub profile, opens in a new tab) linked this to fix #12639
tanstack-source added labelsbug
TkDodo closed this
Sign in to join this conversation.
🦋 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
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