Fix preact selector stability #318
Mergedcrutchcorn(GitHub profile, opens in a new tab)merged 2 commits into
main fromrefs/pull/318/headmerged by crutchcorn(GitHub profile, opens in a new tab) Sign in to join this conversation.
Caution
Review failed#
Pull request was closed or merged during review
📝 Walkthrough
Walkthrough#
The preact-store
useSelectorhook now memoizes selector results based on snapshot identity using a ref-backed cache, preventing unnecessary selector invocations. Both preact and react store implementations include matching test cases confirming the behavior works correctly with array selectors that depend on multiple store fields.Changes#
Selector memoization enhancement
packages/preact-store/src/useSelector.tsmemoizedSelectorcallback returns cached result when snapshot is identity-equal, otherwise recomputes and updates cache.packages/preact-store/src/useSelector.tsuseSyncExternalStoreWithSelectorreceivesmemoizedSelectorinstead of rawselector, preserving subscription and compare logic.packages/preact-store/tests/index.test.tsx,packages/react-store/tests/index.test.tsxuseSelectorhandles array selectors and re-renders when individual source fields update..changeset/fancy-colts-rush.md@tanstack/preact-storenoting thatuseSelectornow handles unstable selector functions.Estimated code review effort#
🎯 2 (Simple) | ⏱️ ~12 minutes
Poem#
🚥 Pre-merge checks | ✅ 3 | ❌ 2
❌ Failed checks (2 warnings)#
✅ Passed checks (3 passed)
✨ 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