Skip to content

fix(tanstack-router): reselect useStore when the selector changes - #971

Merged
leonidaz merged 2 commits into
mainfrom
cursor/fix-usestore-stale-selector-d7be
Sep 6, 2026
Merged

leonidaz merged 2 commits into
mainfrom
cursor/fix-usestore-stale-selector-d7be

Conversation

@jonkwheeler

@jonkwheeler jonkwheeler commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #962

Summary

  • useStore kept the previous selection whenever the atom value was unchanged, even if the selector (or atom/comparator) had changed.
  • Outlet selects the next match id with a selector that captures parentId. After sibling table navigation, matchesId had already published, so the new selector never ran and the nested outlet stayed empty.
  • The snapshot cache now keys on atom, selector, comparator, and input. Equal selected values still reuse the previous reference so allocating selectors do not loop.

Why

  • Navigating /tables/users → /tables/orders left the child outlet empty until reload.
  • The issue's three-step selector-capture sequence reproduced on current main: after publishing ['new', 'new-child'] with parentId still 'old', updating to parentId: 'new' without another publish returned undefined instead of 'new-child'.

Changes

  • packages/tanstack-router/src/useStore.ts: invalidate the snapshot cache when the selector, atom, or comparator changes.
  • Store-selection regressions for captured parent id, atom replacement, and allocating selectors with equality.
  • Outlet integration: sibling parameterized table routes in the basic fixture.

Validation

  • pnpm format:check (not run; used scoped pnpm format:files on the changed paths)
  • pnpm typecheck (not run repo-wide; pnpm typecheck:files on the changed paths reports pre-existing errors in hooks.tsrx / PreviousRouterValueBoundary, not in this diff)
  • pnpm test (not run repo-wide)
  • targeted tests: vitest run --project tanstack-router — 84 passed, including the new store-selection and sibling-table Outlet cases
  • targeted tests: vitest run --project tanstack-router-ssr — 7 passed
  • pre-fix reproduction: the captured-parent-id test returned 'none' instead of 'new-child'; sibling table navigation lost .table-rows after /tables/users → /tables/orders

Risk / follow-ups

  • Inline selectors now miss the input-only fast path every render and rely on compare to keep snapshot identity. That matches Outlet and the previous allocating-selector contract.
  • pnpm sync produced no generated drift.

Reviewers

READY FOR REVIEW. Requested reviews from @leonidaz and @trueadm.

Provenance

  • An agent produced this diff (agent-authored)
Open in Web Open in Cursor 

View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

Cache snapshots by atom, selector, and comparator as well as store input
so a captured parent match id is re-evaluated after navigation publishes
the new match list. Nested Outlets no longer stay empty between sibling
parameterized routes.

Fixes #962

Co-authored-by: Jon Wheeler <[email protected]>
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix labels Sep 6, 2026
@jonkwheeler
jonkwheeler marked this pull request as ready for review September 6, 2026 13:50
@leonidaz
leonidaz merged commit 67ae07f into main Sep 6, 2026
28 checks passed
@leonidaz
leonidaz deleted the cursor/fix-usestore-stale-selector-d7be branch September 6, 2026 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TanStack Router useStore retains stale selection when selector changes without a store update

3 participants