fix(preact-store): recompute selections when selectors change #367
main fromrefs/pull/367/head·merged by KevinVandy 🎯 Changes#
Fix a regression introduced by #318 in @tanstack/preact-store@0.13.1: changing a selector while the store snapshot stays unchanged returns the previous selector's result. For example, useSelector(store, (state) => state[modifier]) keeps returning the Alt selection after modifier changes to Control. This surfaced in TanStack Hotkeys while upgrading the adapter.
Replace the shared snapshot-cache ref with a selector-local closure created by useMemo([selector]). A new selector gets a fresh cache, while repeated reads through the same selector still reuse the result for an unchanged snapshot. This preserves the protection against render loops from selectors returning fresh arrays or objects and retains custom equality behavior.
Add five test cases covering inline and memoized selectors with changing props, stable-selector caching, equality across selector changes, and fresh array selections across parent rerenders and store updates. Four of the new cases fail on the previous implementation; all pass with the fix, including the existing array-selector regression test from #318.
Validation#
CI=1 NX_DAEMON=false pnpm test:prpasses all 17 affected tasks: package tests (55 passing runtime/type test cases), TypeScript 5.6/5.7/5.8/6.0, ESLint, build/publint, workspace checks, and affected Preact example checks/builds.- Prettier and
git diff --checkpass. pnpm changeset statusconfirms a patch release for@tanstack/preact-storeonly.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested this code locally with
pnpm test:pr.
🚀 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 stale selector results when selector logic changes, including when it depends on updated component props, even if the store snapshot has not changed.
- Preserved efficient snapshot caching for selectors that return new arrays or objects, while ensuring selections are recalculated when needed.
- 1
View your CI Pipeline Execution ↗ for commit d9aa1369669db14f60d871686cbbd7930c4a825d
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 1m 40s View ↗ nx run-many --target=build --exclude=examples/**✅ Succeeded <1s View ↗
☁️ Nx Cloud last updated this comment at
2026-09-23 18:33:12UTC@tanstack/angular-store
npm i https://pkg.pr.new/@tanstack/angular-store@367@tanstack/lit-store
npm i https://pkg.pr.new/@tanstack/lit-store@367@tanstack/octane-store
npm i https://pkg.pr.new/@tanstack/octane-store@367@tanstack/preact-store
npm i https://pkg.pr.new/@tanstack/preact-store@367@tanstack/react-store
npm i https://pkg.pr.new/@tanstack/react-store@367@tanstack/solid-store
npm i https://pkg.pr.new/@tanstack/solid-store@367@tanstack/store
npm i https://pkg.pr.new/@tanstack/store@367@tanstack/svelte-store
npm i https://pkg.pr.new/@tanstack/svelte-store@367@tanstack/vue-store
npm i https://pkg.pr.new/@tanstack/vue-store@367commit:
9927252KevinVandy merged this43bb6ee
KevinVandy closed this
Sign in to join this conversation.
Navigate logical layers of code changes, visualize relationships, and explore their blast radius.
Note
Currently processing new changes in this PR. This may take a few minutes, please wait...
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Advanced
Run ID:
f8232f7c-6b6b-439e-bf4c-8f56ba05cfe4📥 Commits
Reviewing files that changed from the base of the PR and between 6ae9e8131b034725be57711f9fddb8d10f59d6f7 and 99272526b025e548cf12806f7da89fa810ab48cc.
📒 Files selected for processing (5)
.changeset/bright-keys-update.mddocs/framework/preact/reference/functions/useSelector.mddocs/framework/preact/reference/interfaces/UseSelectorOptions.mdpackages/preact-store/src/useSelector.tspackages/preact-store/tests/index.test.tsx✨ Finishing Touches
📝 Generate docstrings
🧪 Generate unit tests (beta)
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.
❤️ Share