Skip to content

Commit

Permalink
fix: improve warning for deprecated equalityFn (#1976)
Browse files Browse the repository at this point in the history
* fix: improve warning for deprecated equalityFn

* fix tsc error
  • Loading branch information
dai-shi authored Aug 6, 2023
1 parent 1a9b309 commit a10dd1c
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type WithReact<S extends ReadonlyStoreApi<unknown>> = S & {
getServerState?: () => ExtractState<S>
}

let didWarnAboutEqualityFn = false

export function useStore<S extends WithReact<StoreApi<unknown>>>(
api: S
): ExtractState<S>
Expand All @@ -47,10 +49,15 @@ export function useStore<TState, StateSlice>(
selector: (state: TState) => StateSlice = api.getState as any,
equalityFn?: (a: StateSlice, b: StateSlice) => boolean
) {
if (import.meta.env?.MODE !== 'production' && equalityFn) {
if (
import.meta.env?.MODE !== 'production' &&
equalityFn &&
!didWarnAboutEqualityFn
) {
console.warn(
"[DEPRECATED] Use `createWithEqualityFn` from 'zustand/traditional'. https://github.com/pmndrs/zustand/discussions/1937"
"[DEPRECATED] Use `createWithEqualityFn` instead of `create` or use `useStoreWithEqualityFn` instead of `useStore`. They can be imported from 'zustand/traditional'. https://github.com/pmndrs/zustand/discussions/1937"
)
didWarnAboutEqualityFn = true
}
const slice = useSyncExternalStoreWithSelector(
api.subscribe,
Expand Down

1 comment on commit a10dd1c

@vercel
Copy link

@vercel vercel bot commented on a10dd1c Aug 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.