From 86582be18d07304032ece09b9a1efae594cc3b3b Mon Sep 17 00:00:00 2001 From: zbeyens Date: Wed, 5 Feb 2025 13:10:00 +0100 Subject: [PATCH] fix --- .changeset/strange-trees-drive.md | 5 +++++ README.md | 2 +- packages/zustand-x/README.md | 2 +- .../zustand-x/src/tests/createStore.spec.ts | 18 ++++++++++-------- packages/zustand-x/src/useStore.ts | 15 +++++++++------ 5 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 .changeset/strange-trees-drive.md diff --git a/.changeset/strange-trees-drive.md b/.changeset/strange-trees-drive.md new file mode 100644 index 0000000..4842b9a --- /dev/null +++ b/.changeset/strange-trees-drive.md @@ -0,0 +1,5 @@ +--- +'zustand-x': patch +--- + +Add `useStoreSelect` diff --git a/README.md b/README.md index e38ec0d..9d68ce9 100644 --- a/README.md +++ b/README.md @@ -283,7 +283,7 @@ Access the underlying Zustand store when needed: ```ts // Use the original Zustand hook -const name = useZustandStore(store, (state) => state.name); +const name = useStoreSelect(store, (state) => state.name); // Get the vanilla store const vanillaStore = store.store; diff --git a/packages/zustand-x/README.md b/packages/zustand-x/README.md index e38ec0d..9d68ce9 100644 --- a/packages/zustand-x/README.md +++ b/packages/zustand-x/README.md @@ -283,7 +283,7 @@ Access the underlying Zustand store when needed: ```ts // Use the original Zustand hook -const name = useZustandStore(store, (state) => state.name); +const name = useStoreSelect(store, (state) => state.name); // Get the vanilla store const vanillaStore = store.store; diff --git a/packages/zustand-x/src/tests/createStore.spec.ts b/packages/zustand-x/src/tests/createStore.spec.ts index a366280..1ac6d4c 100644 --- a/packages/zustand-x/src/tests/createStore.spec.ts +++ b/packages/zustand-x/src/tests/createStore.spec.ts @@ -3,10 +3,10 @@ import { devtools } from 'zustand/middleware'; import { createStore } from '../createStore'; import { + useStoreSelect, useStoreState, useStoreValue, useTrackedStore, - useZustandStore, } from '../useStore'; describe('zustandX', () => { @@ -448,16 +448,18 @@ describe('zustandX', () => { }); }); -describe('useZustandStore', () => { - it('should return the underlying Zustand store hook', () => { +describe('useStoreSelect', () => { + it('should use the underlying Zustand store hook', () => { const store = createStore({ name: 'test' }, { name: 'test-store' }); - const useStore = useZustandStore(store); - - // The hook should be the same as store.useStore - expect(useStore).toBe(store.useStore); // Test in a component - const { result } = renderHook(() => useStore((state) => state.name)); + const { result } = renderHook(() => + useStoreSelect( + store, + (state) => state.name, + (a, b) => a === b + ) + ); expect(result.current).toBe('test'); }); }); diff --git a/packages/zustand-x/src/useStore.ts b/packages/zustand-x/src/useStore.ts index a1019c8..1af91a1 100644 --- a/packages/zustand-x/src/useStore.ts +++ b/packages/zustand-x/src/useStore.ts @@ -109,17 +109,20 @@ export function useTracked< } /** - * Get the underlying Zustand store hook. + * Use zustand store selector with optional equality function. * @example - * const name = useZustandStore(store, (state) => state.name) + * const name = useStoreSelect(store, (state) => state.name, equalityFn) */ -export const useZustandStore = < +export const useStoreSelect = < StateType extends TState, Mutators extends [StoreMutatorIdentifier, unknown][], TActions extends Record = {}, TSelectors extends Record = {}, + U = StateType, >( - store: TStateApi -) => { - return store.useStore; + store: TStateApi, + selector: (state: StateType) => U, + equalityFn?: (a: U, b: U) => boolean +): U => { + return store.useStore(selector, equalityFn); };