Skip to content

Commit

Permalink
fix: make selector default to identity function
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
TkDodo committed Dec 30, 2023
1 parent 9c7b13e commit a985abe
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type ReadonlyStoreApi<T> = Pick<StoreApi<T>, 'getState' | 'subscribe'>

let didWarnAboutEqualityFn = false

const identity = <T>(arg: T): T => arg

export function useStore<S extends StoreApi<unknown>>(api: S): ExtractState<S>

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

export function useStore<TState, StateSlice>(
api: StoreApi<TState>,
selector: (state: TState) => StateSlice = api.getState as any,
selector: (state: TState) => StateSlice = identity as any,
equalityFn?: (a: StateSlice, b: StateSlice) => boolean,
) {
if (
Expand Down

0 comments on commit a985abe

Please sign in to comment.