useQueries: stable combine returns a stale result when a query updates before setQueries runs
维护者通常 1 天内回复
@sakaintp 已经在做这个了。
开始于 2026年11月19日。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 62/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- react, typescript
- 领域
- frontend
调研方向
Start in packages/query-core/src/queriesObserver.ts: read #combineResult, #notify and getOptimisticResult in that order, and note that only the last passes queryHashes while #notify does not. Reproduce first by running the gist's repro.mjs against a local build (npm i, then node repro.mjs) and confirm the final line prints 3 instead of 5. Done means the combined result tracks the current query list and a regression test alongside the existing QueriesObserver tests covers the case where a query updates between render and setQueries.
由索引模型根据 Issue 内容生成。
描述
[!NOTE]
🤖 Claude Opus 5.5 responding on behalf of swheel33
Describe the bug
useQueries with a stable combine can return a combined result built from the previous query list, so its length no longer matches the queries passed in on that render. Code that indexes the combined result by the input list (results[index].data) then throws: we hit TypeError: Cannot read properties of undefined in our app.
This is a gap left by #9954 (the fix for #8781). #combineResult now detects a changed query list by comparing queryHashes with #lastQueryHashes, but only getOptimisticResult passes queryHashes. #notify calls #combineResult without them, so it recomputes from the observer's current results (the old list) while #lastQueryHashes still holds the new list from the last render. On the next render the hashes look unchanged, #result === #lastResult, and the stale combined result is returned.
Sequence:
- Render with queries A (3 queries). The effect calls
setQueries(A). - Render with queries B (5 queries).
getOptimisticResultcombines 5 results and sets#lastQueryHashes = B. - Before the effect calls
setQueries(B), a query in A updates.#onUpdate→#notify→#combineResultcombines the 3 results of A and caches them, leaving#lastQueryHashes = B. - The listener re-renders with B.
#combineResultsees no change and returns the 3-item result.
Your minimal, reproducible example
https://gist.github.com/swheel33/2f38c4065d592f58a8046917c405252e
Steps to reproduce
npm i @tanstack/[email protected]
node repro.mjs
Output:
render with 3 queries -> 3
render with 5 queries -> 5
re-render with 5 queries -> 3 (expected 5)
The script drives QueriesObserver the way useQueries does: getOptimisticResult per render, setQueries in an effect.
Expected behavior
The combined result reflects the queries passed on the current render, so the last line prints 5.
How often does this bug happen?
Sometimes
Platform
@tanstack/query-core5.108.0 (also seen on 5.103.0)@tanstack/react-query5.103.0, React 19- OS: macOS, Browser: Chrome
Tanstack Query adapter
react-query
TanStack Query version
5.108.0
TypeScript version
N/A (the repro is plain JS)
Additional context
In the browser this happens when the list of queries grows (in our case, one balance query per token, and the token list grows when a wallet connects) and an existing query resolves between the render and the useQueries effect.
We worked around it with a local patch that has #notify pass the hashes of the results it combines, so the next render sees the list changed:
this.#combineResult(
this.#trackResult(this.#result, this.#observerMatches),
this.#options?.combine,
this.#observerMatches.map((match) => match.defaultedQueryOptions.queryHash),
)
- 主要语言
- TypeScript
- 星标
- 50.6k
- 派生
- 4.2k
- 平均合并
- 9 小时 50 分钟
- 30 天内合并 PR
- 310
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
TanStack/query 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
TanStack/query#12466 · 1 条评论 ·
维护者通常 1 天内回复
-
solid-query: STRICT_READ_UNTRACKED on Solid 2 — client()/options() read in component body (useMutation, useBaseQuery)可能重新可做 @LeonxLJX 于 77 天前认领,目前没有进行中的 PR。 未关闭
难度 2/5 1-3 小时 新手友好度 84/100
TanStack/query#11358 · 2 条评论 · 1 个 reaction ·
维护者通常 1 天内回复
-
select that throws for placeholderData brings back the selection of data removed by resetQueries可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 3/5 1-2 天 新手友好度 56/100
维护者通常 1 天内回复
-
Render loop since v5.105.2 when placeholderData, or its select result, is not referentially stable未关闭
难度 4/5 3-5 天 新手友好度 54/100
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 25/100
维护者通常 1 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
fb55/htmlparser2#2579 ·
维护者通常 1 天内回复
-
bug good first issue needs-review
难度 1/5 1-3 小时 新手友好度 83/100
维护者通常 1 天内回复
-
documentation public
难度 1/5 1-3 小时 新手友好度 68/100
midnightntwrk/umbradb#65 ·
维护者通常 2 天内回复
-
area/channels kind/bug priority/medium
难度 2/5 1-3 小时 新手友好度 78/100
nanocoai/nanoclaw#4501 · 1 条评论 ·
维护者通常 1 天内回复
-
needs verification
难度 2/5 1-3 小时 新手友好度 74/100
维护者通常 13 天内回复