Skip to content

Commit a985abe

Browse files
committed
fix: make selector default to identity function
if we default to `api.getState`, we will always read the client snapshot if there is no selector passed. An identity function returns its argument, which is either the snapshot (api.getState) or the server snapshot (api.getServerState)
1 parent 9c7b13e commit a985abe

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/react.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ type ReadonlyStoreApi<T> = Pick<StoreApi<T>, 'getState' | 'subscribe'>
2424

2525
let didWarnAboutEqualityFn = false
2626

27+
const identity = <T>(arg: T): T => arg
28+
2729
export function useStore<S extends StoreApi<unknown>>(api: S): ExtractState<S>
2830

2931
export function useStore<S extends StoreApi<unknown>, U>(
@@ -43,7 +45,7 @@ export function useStore<S extends StoreApi<unknown>, U>(
4345

4446
export function useStore<TState, StateSlice>(
4547
api: StoreApi<TState>,
46-
selector: (state: TState) => StateSlice = api.getState as any,
48+
selector: (state: TState) => StateSlice = identity as any,
4749
equalityFn?: (a: StateSlice, b: StateSlice) => boolean,
4850
) {
4951
if (

0 commit comments

Comments
 (0)