Skip to content

Fix preact selector stability#318

Merged
crutchcorn merged 2 commits into
mainfrom
fix-preact-selector-stability
May 8, 2026
Merged

Fix preact selector stability#318
crutchcorn merged 2 commits into
mainfrom
fix-preact-selector-stability

Conversation

@crutchcorn
Copy link
Copy Markdown
Member

@crutchcorn crutchcorn commented May 8, 2026

This PR fixes the ability to run non-stable Preact selectors in useSelector; preventing infinite loops

Summary by CodeRabbit

  • Bug Fixes

    • useSelector now properly handles unstable selector functions, reducing unnecessary component re-renders and improving performance when utilizing non-memoized selectors.
  • Tests

    • Added test coverage to verify useSelector correctly processes complex selector patterns, including those returning arrays and other data structures.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 8, 2026

Review Change Stack

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

The preact-store useSelector hook 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

Layer / File(s) Summary
Memoization mechanism
packages/preact-store/src/useSelector.ts
A ref stores the last snapshot and selected value; memoizedSelector callback returns cached result when snapshot is identity-equal, otherwise recomputes and updates cache.
Hook integration
packages/preact-store/src/useSelector.ts
useSyncExternalStoreWithSelector receives memoizedSelector instead of raw selector, preserving subscription and compare logic.
Test coverage
packages/preact-store/tests/index.test.tsx, packages/react-store/tests/index.test.tsx
Preact and react tests verify useSelector handles array selectors and re-renders when individual source fields update.
Release documentation
.changeset/fancy-colts-rush.md
Changeset documents a patch bump for @tanstack/preact-store noting that useSelector now handles unstable selector functions.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 A cache for the selectors so keen,
No snapshot alike shall be seen,
Arrays dance twice,
With memoized splice,
The preact store now runs lean!

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description explains the fix briefly, but does not follow the provided template structure with sections for 'Changes' and 'Checklist' or 'Release Impact'. Update the description to follow the repository template: add a '## 🎯 Changes' section with details, and include the '## ✅ Checklist' and '## 🚀 Release Impact' sections with appropriate checkmarks.
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Fix preact selector stability' is directly related to the main change, which is fixing preact selector stability by memoizing selectors to prevent infinite loops.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-preact-selector-stability

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 and usage tips.

@nx-cloud
Copy link
Copy Markdown

nx-cloud Bot commented May 8, 2026

View your CI Pipeline Execution ↗ for commit 1ce4418

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 34s View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded 2s View ↗

☁️ Nx Cloud last updated this comment at 2026-05-08 20:50:21 UTC

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 8, 2026

🚀 Changeset Version Preview

1 package(s) bumped directly, 0 bumped as dependents.

🟩 Patch bumps

Package Version Reason
@tanstack/preact-store 0.13.0 → 0.13.1 Changeset

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 8, 2026

@tanstack/angular-store

npm i https://pkg.pr.new/@tanstack/angular-store@318

@tanstack/preact-store

npm i https://pkg.pr.new/@tanstack/preact-store@318

@tanstack/react-store

npm i https://pkg.pr.new/@tanstack/react-store@318

@tanstack/solid-store

npm i https://pkg.pr.new/@tanstack/solid-store@318

@tanstack/store

npm i https://pkg.pr.new/@tanstack/store@318

@tanstack/svelte-store

npm i https://pkg.pr.new/@tanstack/svelte-store@318

@tanstack/vue-store

npm i https://pkg.pr.new/@tanstack/vue-store@318

commit: 1ce4418

@crutchcorn crutchcorn merged commit 4e0c7ef into main May 8, 2026
6 of 7 checks passed
@crutchcorn crutchcorn deleted the fix-preact-selector-stability branch May 8, 2026 20:51
@github-actions github-actions Bot mentioned this pull request May 8, 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