Skip to content

Commit 51a7555

Browse files
committed
fix(react-form): restore optional selector in useStore re-export
Prior to @tanstack/react-store 0.9.1, useStore could be called with just a store reference and no selector, which returned the full atom snapshot: const formState = useStore(form.store) The 0.9.1 update made the selector argument required in TypeScript types, causing a build error in existing code using the single-argument form. Instead of re-exporting useStore directly, add a thin overload wrapper in packages/react-form/src/useStore.ts that makes the selector optional and preserves backward-compatible types while delegating to the underlying @tanstack/react-store implementation. Fixes #2074
1 parent b1bf05d commit 51a7555

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

packages/react-form/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export * from '@tanstack/form-core'
22

3-
export { useStore } from '@tanstack/react-store'
3+
export { useStore } from './useStore'
44

55
export * from './createFormHook'
66
export * from './types'
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { useStore as _useStore } from '@tanstack/react-store'
2+
import type { AnyAtom } from '@tanstack/store'
3+
4+
type AtomSnapshot<TAtom> = TAtom extends { get: () => infer TSnapshot }
5+
? TSnapshot
6+
: undefined
7+
8+
/**
9+
* Subscribe to a store atom and return its current state.
10+
*
11+
* When called without a `selector` the full atom snapshot is returned.
12+
*
13+
* @example
14+
* // Return the full form state (selector optional for back-compat)
15+
* const formState = useStore(form.store)
16+
*
17+
* @example
18+
* // Return a specific slice
19+
* const isValid = useStore(form.store, (s) => s.isValid)
20+
*/
21+
export function useStore<TAtom extends AnyAtom | undefined>(
22+
atom: TAtom,
23+
): AtomSnapshot<TAtom>
24+
25+
export function useStore<TAtom extends AnyAtom | undefined, T>(
26+
atom: TAtom,
27+
selector: (snapshot: AtomSnapshot<TAtom>) => T,
28+
compare?: (a: T, b: T) => boolean,
29+
): T
30+
31+
export function useStore<TAtom extends AnyAtom | undefined, T>(
32+
atom: TAtom,
33+
selector?: (snapshot: AtomSnapshot<TAtom>) => T,
34+
compare?: (a: T, b: T) => boolean,
35+
): T | AtomSnapshot<TAtom> {
36+
// When no selector is provided fall back to the identity function so that
37+
// callers that were using `useStore(form.store)` without a selector
38+
// (as was valid in prior releases) continue to work.
39+
return _useStore(
40+
atom,
41+
(selector ?? ((s: any) => s)) as (snapshot: AtomSnapshot<TAtom>) => T,
42+
compare,
43+
)
44+
}

0 commit comments

Comments
 (0)