Skip to content

Commit 86582be

Browse files
committed
fix
1 parent 6991af1 commit 86582be

File tree

5 files changed

+26
-16
lines changed

5 files changed

+26
-16
lines changed

.changeset/strange-trees-drive.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'zustand-x': patch
3+
---
4+
5+
Add `useStoreSelect`

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ Access the underlying Zustand store when needed:
283283

284284
```ts
285285
// Use the original Zustand hook
286-
const name = useZustandStore(store, (state) => state.name);
286+
const name = useStoreSelect(store, (state) => state.name);
287287

288288
// Get the vanilla store
289289
const vanillaStore = store.store;

packages/zustand-x/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ Access the underlying Zustand store when needed:
283283

284284
```ts
285285
// Use the original Zustand hook
286-
const name = useZustandStore(store, (state) => state.name);
286+
const name = useStoreSelect(store, (state) => state.name);
287287

288288
// Get the vanilla store
289289
const vanillaStore = store.store;

packages/zustand-x/src/tests/createStore.spec.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { devtools } from 'zustand/middleware';
33

44
import { createStore } from '../createStore';
55
import {
6+
useStoreSelect,
67
useStoreState,
78
useStoreValue,
89
useTrackedStore,
9-
useZustandStore,
1010
} from '../useStore';
1111

1212
describe('zustandX', () => {
@@ -448,16 +448,18 @@ describe('zustandX', () => {
448448
});
449449
});
450450

451-
describe('useZustandStore', () => {
452-
it('should return the underlying Zustand store hook', () => {
451+
describe('useStoreSelect', () => {
452+
it('should use the underlying Zustand store hook', () => {
453453
const store = createStore({ name: 'test' }, { name: 'test-store' });
454-
const useStore = useZustandStore(store);
455-
456-
// The hook should be the same as store.useStore
457-
expect(useStore).toBe(store.useStore);
458454

459455
// Test in a component
460-
const { result } = renderHook(() => useStore((state) => state.name));
456+
const { result } = renderHook(() =>
457+
useStoreSelect(
458+
store,
459+
(state) => state.name,
460+
(a, b) => a === b
461+
)
462+
);
461463
expect(result.current).toBe('test');
462464
});
463465
});

packages/zustand-x/src/useStore.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,20 @@ export function useTracked<
109109
}
110110

111111
/**
112-
* Get the underlying Zustand store hook.
112+
* Use zustand store selector with optional equality function.
113113
* @example
114-
* const name = useZustandStore(store, (state) => state.name)
114+
* const name = useStoreSelect(store, (state) => state.name, equalityFn)
115115
*/
116-
export const useZustandStore = <
116+
export const useStoreSelect = <
117117
StateType extends TState,
118118
Mutators extends [StoreMutatorIdentifier, unknown][],
119119
TActions extends Record<string, AnyFunction> = {},
120120
TSelectors extends Record<string, AnyFunction> = {},
121+
U = StateType,
121122
>(
122-
store: TStateApi<StateType, Mutators, TActions, TSelectors>
123-
) => {
124-
return store.useStore;
123+
store: TStateApi<StateType, Mutators, TActions, TSelectors>,
124+
selector: (state: StateType) => U,
125+
equalityFn?: (a: U, b: U) => boolean
126+
): U => {
127+
return store.useStore(selector, equalityFn);
125128
};

0 commit comments

Comments
 (0)