Skip to content

fix(preact-store): recompute selections when selectors change - #367

Merged
KevinVandy merged 2 commits into
mainfrom
fix/preact-selector-cache
Sep 24, 2026
Merged

KevinVandy merged 2 commits into
mainfrom
fix/preact-selector-cache

Conversation

@KevinVandy

@KevinVandy KevinVandy commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

🎯 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:pr passes 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 --check pass.
  • pnpm changeset status confirms a patch release for @tanstack/preact-store only.

✅ 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.

@coderabbitai

coderabbitai Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

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 6ae9e81 and 9927252.

📒 Files selected for processing (5)
  • .changeset/bright-keys-update.md
  • docs/framework/preact/reference/functions/useSelector.md
  • docs/framework/preact/reference/interfaces/UseSelectorOptions.md
  • packages/preact-store/src/useSelector.ts
  • packages/preact-store/tests/index.test.tsx
 _______________________________________
< We're gonna need a bigger bug zapper. >
 ---------------------------------------
  \
   \   \
        \ /\
        ( )
      .( o ).
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR

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

Comment @coderabbitai help to get the list of available commands.

@nx-cloud

nx-cloud Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit d9aa136

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:12 UTC

@pkg-pr-new

pkg-pr-new Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
@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@367

commit: 9927252

@KevinVandy
KevinVandy marked this pull request as ready for review September 24, 2026 14:22
@KevinVandy
KevinVandy merged commit 43bb6ee into main Sep 24, 2026
8 of 9 checks passed
@KevinVandy
KevinVandy deleted the fix/preact-selector-cache branch September 24, 2026 14:24
@github-actions github-actions Bot mentioned this pull request Sep 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant