Skip to content
Source
Sign inSign up
TanStack/store

fix(svelte-store): use $state.raw in useSelector to prevent proxy equality mismatch #334

Closedseongwon030(GitHub profile, opens in a new tab)wants to merge 3 commits intomain fromrefs/pull/334/headopened

Showing 5 changed files with 43 additions and 1 deletion (loading 2).

x
0 of 5 viewed, 5 left
5 additions and 0 deletions.changeset/fix-svelte-store-proxy-equality.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/svelte-store': patch
+---
+​
+Fix `state_proxy_equality_mismatch` warning in `useSelector` by using `$state.raw()` instead of `$state()` for the slice variable. `$state()` wrapped object values in a Svelte Proxy, causing `===` comparison with the raw selector output to always fail, which triggered unnecessary re-renders and Svelte runtime warnings on every store update.
3 additions and 1 deletionpackages/svelte-store/src/useSelector.svelte.tsModified
View file
@@ -35,7 +35,9 @@
options: UseSelectorOptions<TSelected> = {},
): { readonly current: TSelected } {
const compare = options.compare ?? defaultCompare
- let slice = $state(selector(source.get()))
+ // `$state.raw` keeps the slice unproxied; a proxied value would never be `===`
+ // to the plain object the selector returns, defeating the equality check below.
+ let slice = $state.raw(selector(source.get()))
​
$effect(() => {
const unsub = source.subscribe((s) => {
35 additions and 0 deletionspackages/svelte-store/tests/ProxyEquality.test.svelteAdded
View file
@@ -0,0 +1,35 @@