Skip to content
Source
Sign inSign up
TanStack/store

Fix preact selector stability #318

Mergedcrutchcorn(GitHub profile, opens in a new tab)merged 2 commits intomain fromrefs/pull/318/headmerged by crutchcorn(GitHub profile, opens in a new tab)

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.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      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 nameStatusExplanationResolution
      Description check⚠️ WarningThe 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⚠️ WarningDocstring 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 nameStatusExplanation
      Title check✅ PassedThe 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✅ PassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes check✅ PassedCheck 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.
    2. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit 1ce4418af659625b72732b882f431341e461d877

      CommandStatusDurationResult
      nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded34sView ↗
      nx run-many --target=build --exclude=examples/**✅ Succeeded2sView ↗

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

    3. 🚀 Changeset Version Preview

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

      🟩 Patch bumps

      PackageVersionReason
      @tanstack/preact-store0.13.0 → 0.13.1Changeset
    4. @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

Sign in to join this conversation.