fix(svelte-store): use $state.raw in useSelector to prevent proxy equality mismatch#334
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
✅ Files skipped from review due to trivial changes (1)
📝 WalkthroughWalkthroughThis PR fixes a state_proxy_equality_mismatch in useSelector by replacing ChangesuseSelector Svelte proxy equality fix
🎯 2 (Simple) | ⏱️ ~10 minutes
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint install failed: lockfile failed supply-chain policy check. Run 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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/svelte-store/src/useSelector.svelte.ts (1)
38-38: ⚡ Quick winConsider adding an inline comment to explain the use of
$state.raw().Future maintainers might wonder why
$state.raw()is used instead of$state(). A brief comment explaining that raw prevents proxy wrapping and ensures correct equality checks would improve maintainability.📝 Suggested comment
const compare = options.compare ?? defaultCompare - let slice = $state.raw(selector(source.get())) + // Use $state.raw() to prevent Svelte Proxy wrapping, ensuring === comparison + // with raw selector output works correctly (prevents state_proxy_equality_mismatch) + let slice = $state.raw(selector(source.get()))🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/svelte-store/src/useSelector.svelte.ts` at line 38, Add a brief inline comment above the expression using $state.raw(selector(source.get())) explaining why .raw() is used (to bypass proxy wrapping so selector equality checks compare plain values rather than proxies), e.g., reference the $state.raw call and the selector/source.get usage in the same line so maintainers see that raw prevents proxy-wrapped results from breaking equality checks in the slice variable assignment.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@packages/svelte-store/src/useSelector.svelte.ts`:
- Line 38: Add a brief inline comment above the expression using
$state.raw(selector(source.get())) explaining why .raw() is used (to bypass
proxy wrapping so selector equality checks compare plain values rather than
proxies), e.g., reference the $state.raw call and the selector/source.get usage
in the same line so maintainers see that raw prevents proxy-wrapped results from
breaking equality checks in the slice variable assignment.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: da0d8182-7e6e-40ae-aa67-b03109e64bfb
📒 Files selected for processing (4)
.changeset/fix-svelte-store-proxy-equality.mdpackages/svelte-store/src/useSelector.svelte.tspackages/svelte-store/tests/ProxyEquality.test.sveltepackages/svelte-store/tests/index.test.ts
…ality mismatch Closes TanStack#322
050f9b9 to
637447a
Compare
🎯 Changes
Fixes #322
$state()wraps object values in a Svelte Proxy. Whenselector(s)returns the sameraw object reference,
===comparison with the Proxy always fails — triggeringstate_proxy_equality_mismatchwarnings and unnecessary re-renders even when theselected value hasn't changed.
Existing tests only covered primitive selectors, which are not Proxy-wrapped. A new test
with an object selector reproduces the issue and verifies the fix.
Switching to
$state.raw()is safe sinceuseSelectoronly ever reassignsslice,never mutates its properties.
✅ Checklist
pnpm test:pr.🚀 Release Impact
Summary by CodeRabbit