useSelector wraps selected objects in $state, causing state_proxy_equality_mismatch and defeating identity comparison
维护者通常 1 天内回复
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 85/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- typescript
- 领域
- frontend
调研方向
从 useSelector.svelte.js 开始,检查所选 slice 与 defaultCompare 一起进行的初始化。使用对象值的 state 以及 issue 中描述的表单交互重现 selector,然后添加承诺的测试。当未改变的对象选择保留身份比较,并且不再出现 state_proxy_equality_mismatch 警告时,即表示完成。
由索引模型根据 Issue 内容生成。
描述
Describe the bug
The Svelte adapter's useSelector initializes its selected slice with $state(...) while its default comparator uses strict identity equality:
function defaultCompare(a, b) {
return a === b
}
export function useSelector(source, selector = (d) => d, options = {}) {
const compare = options.compare ?? defaultCompare
let slice = $state(selector(source.get()))
$effect(() => {
const unsub = source.subscribe((s) => {
const data = selector(s)
if (compare(slice, data)) return
slice = data
})
return () => {
unsub()
}
})
return () => slice
}
When selector(source.get()) returns an object or array, $state(...) deep-proxies that value.
The initial slice is therefore a Svelte proxy, while subsequent data values from the TanStack store are raw objects.
The default comparison then effectively does:
proxy === raw
which can never be true, even when both represent the same underlying object.
Svelte reports:
[svelte] state_proxy_equality_mismatch
Reactive $state(...) proxies and the values they proxy have different
identities. Because of this, comparisons with === will produce
unexpected results.
This is especially visible in TanStack Form because Field.svelte uses useSelector for object-valued state such as:
state.meta.errorMap
state.meta.errorSourceMap
array/object field values
As a result, normal form operations such as opening/closing a form, resetting it, submitting it, or updating a field can generate multiple state_proxy_equality_mismatch warnings.
This also means the issue is not purely diagnostic noise: because the strict comparison returns false, the selector invalidates again on store notifications even when the selected raw object identity has not actually changed.
Changing the initialization to $state.raw(...) appears to resolve both problems:
- let slice = $state(selector(source.get()))
- let slice = $state.raw(selector(source.get()))
This seems consistent with the semantics of this selector: TanStack Store replaces selected objects rather than requiring Svelte to deeply track mutations to the selected value, while defaultCompare explicitly relies on object identity.
Your minimal, reproducible example
Will include tests in my PR.
Steps to reproduce
- Create a Svelte application using @tanstack/svelte-form.
- Create a form with at least one field.
- Mount the field so its TanStack state selectors are subscribed.
- Trigger form state notifications, for example by changing a field, resetting the form, or submitting it.
- Open the browser console.
- Observe one or more Svelte state_proxy_equality_mismatch warnings.
- Inspect the stack trace.
- The warning reaches:
state_proxy_equality_mismatch
strict_equals
defaultCompare
useSelector.svelte.js - Patch useSelector.svelte.js from:
let slice = $state(selector(source.get()))
to:
let slice = $state.raw(selector(source.get())) - Repeat the same interactions.
- The proxy equality warnings disappear and unchanged object selections can correctly satisfy the identity comparator.
Expected behavior
useSelector should preserve the identity semantics expected by its default a === b comparator.
If a selector returns an object and the store subsequently provides that same object reference, the comparison should be capable of returning true.
Using the adapter should not cause Svelte's state_proxy_equality_mismatch warning during normal form interactions.
Ideally, an unchanged object-valued selection should also not cause an unnecessary reactive invalidation.
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
Windows
Chrome
TanStack Form adapter
svelte-form
TanStack Form version
1.33.5
TypeScript version
6.0.3
Additional context
No response
- 主要语言
- TypeScript
- 星标
- 907
- 派生
- 116
- 平均合并
- 1 天 7 小时
- 30 天内合并 PR
- 14
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
TanStack/store 的其他 Issue
-
`useSelector` triggers `state_proxy_equality_mismatch` on every store update in svelte-store可能重新可做 关联的 PR 已关闭且未合并。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
TanStack/store#322 · 3 条评论 · 5 个 reaction ·
维护者通常 1 天内回复
-
Missing atom docs可能已有人在做 @looooown2006 于 165 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 65/100
维护者通常 1 天内回复
-
Reading a computed atom without subscribing can prevent garbage collection可能已有人在做 @Sheraff 于 21 天前认领。 未关闭
难度 4/5 3-5 天 新手友好度 58/100
维护者通常 1 天内回复
-
难度 3/5 1-2 天 新手友好度 45/100
维护者通常 1 天内回复
-
Type error in `useStore` when upgrading from 1.28.0 to 1.28.4可能重新可做 关联的 PR 已关闭且未合并。 未关闭
难度 3/5 1-2 天 新手友好度 52/100
TanStack/store#323 · 2 条评论 · 1 个 reaction ·
维护者通常 1 天内回复
相似的 Issue
-
enhancement
难度 2/5 1-3 小时 新手友好度 68/100
equinor/prisma-decision-web#919 ·
维护者通常 1 天内回复
-
[DC] OneLogin sync imports a partial user list when an API request fails可能已有人在做 @RaphaelFakhri 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 84/100
bitwarden/directory-connector#1276 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
home-assistant/installer#324 ·
维护者通常 1 天内回复
-
Mondriaan
难度 2/5 1-3 小时 新手友好度 65/100
knaw-huc/textannoviz#733 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复