Skip to content

Commit

Permalink
update readme helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
charkour committed Nov 17, 2024
1 parent 17a8f17 commit 0efc896
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
zustand: [
4.2.0, # Oldest zustand TS supported
4.0.0, # Oldest zustand JS supported
4, # Latest zustand v5 supported
4, # Latest zustand v5 supported
latest # Latest zustand supported
]

Expand Down
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Try a live [demo](https://codesandbox.io/s/currying-flower-2dom9?file=/src/App.t
npm i zustand zundo
```

> zustand v4.2.0 or higher is required for TS usage. v4.0.0 or higher is required for JS usage.
> zustand v4.2.0+ or v5 is required for TS usage. v4.0.0 or higher is required for JS usage.
> Node 16 or higher is required.
## Background
Expand Down Expand Up @@ -87,13 +87,21 @@ const App = () => {
In React, to subscribe components or custom hooks to member properties of the `temporal` object (like the array of `pastStates` or `currentStates`), you can create a `useTemporalStore` hook.

```tsx
import { useStore } from 'zustand';
import { useStoreWithEqualityFn } from 'zustand/traditional';
import type { TemporalState } from 'zundo';

const useTemporalStore = <T,>(
selector: (state: TemporalState<StoreState>) => T,
function useTemporalStore(): TemporalState<MyState>;
function useTemporalStore<T>(selector: (state: TemporalState<MyState>) => T): T;
function useTemporalStore<T>(
selector: (state: TemporalState<MyState>) => T,
equality: (a: T, b: T) => boolean,
): T;
function useTemporalStore<T>(
selector?: (state: TemporalState<MyState>) => T,
equality?: (a: T, b: T) => boolean,
) => useStore(useStoreWithUndo.temporal, selector, equality);
) {
return useStoreWithEqualityFn(originalStore.temporal, selector!, equality);
}

const App = () => {
const { bears, increasePopulation, removeAllBears } = useStoreWithUndo();
Expand Down Expand Up @@ -558,6 +566,7 @@ PRs are welcome! [pnpm](https://pnpm.io/) is used as a package manager. Run `pnp
- [SubscribeWithSelector](https://codesandbox.io/s/zundo-with-subscribe-with-selector-forked-mug69t)
- [canUndo, canRedo, undoDepth, redoDepth](https://codesandbox.io/s/zundo-canundo-and-undodepth-l6jclx?file=/src/App.tsx:572-731)
- [with deep equal](https://codesandbox.io/p/sandbox/zundo-deep-equal-qg69lj)
- [with input](https://stackblitz.com/edit/vitejs-vite-jqngm9?file=src%2FApp.tsx)

## Migrate from v1 to v2

Expand Down
13 changes: 11 additions & 2 deletions examples/web/pages/diff.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,19 @@ const useBaseStore = <T,>(
selector: (state: MyState) => T,
equality?: (a: T, b: T) => boolean,
) => useStoreWithEqualityFn(originalStore, selector, equality);
const useTemporalStore = <T,>(

function useTemporalStore(): TemporalState<MyState>;
function useTemporalStore<T>(selector: (state: TemporalState<MyState>) => T): T;
function useTemporalStore<T>(
selector: (state: TemporalState<MyState>) => T,
equality: (a: T, b: T) => boolean,
): T;
function useTemporalStore<T>(
selector?: (state: TemporalState<MyState>) => T,
equality?: (a: T, b: T) => boolean,
) => useStoreWithEqualityFn(originalStore.temporal, selector, equality);
) {
return useStoreWithEqualityFn(originalStore.temporal, selector!, equality);
}

const isEmpty = (obj: object) => {
for (const _ in obj) {
Expand Down

0 comments on commit 0efc896

Please sign in to comment.