Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zbeyens committed Feb 5, 2025
1 parent 6991af1 commit 86582be
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-trees-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'zustand-x': patch
---

Add `useStoreSelect`
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/zustand-x/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 10 additions & 8 deletions packages/zustand-x/src/tests/createStore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { devtools } from 'zustand/middleware';

import { createStore } from '../createStore';
import {
useStoreSelect,
useStoreState,
useStoreValue,
useTrackedStore,
useZustandStore,
} from '../useStore';

describe('zustandX', () => {
Expand Down Expand Up @@ -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');
});
});
15 changes: 9 additions & 6 deletions packages/zustand-x/src/useStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, AnyFunction> = {},
TSelectors extends Record<string, AnyFunction> = {},
U = StateType,
>(
store: TStateApi<StateType, Mutators, TActions, TSelectors>
) => {
return store.useStore;
store: TStateApi<StateType, Mutators, TActions, TSelectors>,
selector: (state: StateType) => U,
equalityFn?: (a: U, b: U) => boolean
): U => {
return store.useStore(selector, equalityFn);
};

0 comments on commit 86582be

Please sign in to comment.