Skip to content

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

Closed

Description

@austinm911

Astra below

useStore returns stale selection when selector changes without a store update

Affected package: @octanejs/tanstack-router 0.1.52 with octane 0.2.3. The same input-only cache is present on upstream main in packages/tanstack-router/src/useStore.ts at time of inspection.

Problem

useStore returns the previous selection whenever Object.is(previousInput, atom.get()) is true. It does not account for a changed selector. A selector that captures a route parameter can therefore retain the selection computed for the previous parameter.

Outlet selects the child match ID using a captured parentId. In the observed application, navigating between sibling table routes changes the URL but leaves the outlet empty until a reload.

Minimal reproduction sequence

  1. Mount a component using useStore with an atom containing ['old', 'old-child']. Its selector returns the ID after the captured parentId, initially 'old'. Result: 'old-child'.
  2. Publish ['new', 'new-child'] to the atom while the captured parentId is still 'old'. Selection becomes undefined.
  3. Render the component with parentId 'new', creating a new selector, without publishing another atom value.
  4. Expected: 'new-child'. Actual: undefined, because the unchanged atom value bypasses the new selector.

This sequence was reproduced with the installed hook mounted through Octane createRoot and act in happy-dom. The original implementation fails. A diagnostic cache keyed by atom, selector, comparator, and input passes.

Proposed correction

Invalidate selection memoization when the selector, atom, or comparator changes. Preserve stable snapshot identity for repeated reads and preserve the prior selected reference when the comparator reports equality. Simply removing snapshot memoization causes an infinite-update warning for allocating selectors.

Regression coverage should include changed selector captures with unchanged input, changed atom identity, allocating selectors with equality comparison, and actual Outlet navigation.

Activity

  1. austinm911 commented on Sep 5, 2026

    @austinm911
    ContributorAuthor

    Proposed correction in packages/tanstack-router/src/useStore.ts, tested locally against the mounted selector reproduction and sibling-route navigation in Chrome:

    diff --git a/src/useStore.ts b/src/useStore.ts
    index 4c206dee434ce7a5a04cd3a25c2591d1629a8592..7cb8ebef6b76e53bf45565dac5bf2f76f005f8e7 100644
    --- a/src/useStore.ts
    +++ b/src/useStore.ts
    @@ -45,20 +45,25 @@ export function useStore(...args: any[]): any {
     		subSlot(slot, 'us:cb'),
     	);
     
    -	// Memoize selector output: same store input → same output; structurally-equal
    -	// output keeps its previous reference (so useSyncExternalStore doesn't loop).
    -	const cache = useRef<{ in: unknown; out: unknown } | null>(null, subSlot(slot, 'us:cache'));
    +	// A cached snapshot belongs to its atom and selector as well as its input.
    +	// Route parameters captured by a new selector must be re-evaluated even
    +	// when the atom has already published the current match-id list.
    +	const cache = useRef<{
    +		atom: Atom<unknown>;
    +		in: unknown;
    +		selector: typeof selector;
    +		compare: typeof compare;
    +		out: unknown;
    +	} | null>(null, subSlot(slot, 'us:cache'));
     	const getSnapshot = (): unknown => {
     		const input = atom.get();
     		const prev = cache.current;
    -		if (prev && Object.is(prev.in, input)) return prev.out;
    +		if (prev && prev.atom === atom && prev.selector === selector &&
    +			prev.compare === compare && Object.is(prev.in, input)) return prev.out;
     		const next = selector(input);
    -		if (prev && compare(prev.out, next)) {
    -			cache.current = { in: input, out: prev.out };
    -			return prev.out;
    -		}
    -		cache.current = { in: input, out: next };
    -		return next;
    +		const out = prev && compare(prev.out, next) ? prev.out : next;
    +		cache.current = { atom, in: input, selector, compare, out };
    +		return out;
     	};
     
     	return useSyncExternalStore(subscribe, getSnapshot, getSnapshot, subSlot(slot, 'us:uses'));

    The cache key includes atom, selector, comparator, and input identity. Repeated reads of the same snapshot stay stable, and equal selected values retain their previous reference. Removing memoization entirely produced an infinite-update warning with allocating selectors, so that is not sufficient.

    This is a proposed fix, not a substitute for the binding's full test suite. Regression coverage should include selector-capture changes without a new store value, replacing the atom, comparator changes, allocating selectors with equality comparison, and Outlet navigation.

  2. added a commit that references this issue on Sep 6, 2026
    67ae07f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions