charkour / zundo

🍜 undo/redo middleware for zustand. <700 bytes
https://codesandbox.io/s/zundo-2dom9
MIT License
597 stars 19 forks source link

Zundo clear is not working #160

Closed tmanager2025 closed 7 months ago

tmanager2025 commented 7 months ago

the clear function is not working. Sandbox: https://codesandbox.io/p/sandbox/zundo-forked-3sjgk9

Code:

import { temporal, TemporalState } from "zundo";
import { create, useStore } from "zustand";
import "./styles.css";

interface MyState {
  bears: number;
  increment: () => void;
  decrement: () => void;
}

const useMyStore = create(
  temporal<MyState>((set) => ({
    bears: {},
    increment: () => set((state) => ({ bears: { a: Math.random() } })),
    decrement: () =>
      set((state) => ({
        bears: { a: Math.random(), def: { r: Math.random() } },
      })),
  })),
);

const App = () => {
  const store = useMyStore();
  const { bears, increment, decrement } = store;
  const { undo, redo, futureStates, pastStates, clear } =
    useMyStore.temporal.getState();

  // const { undo, redo, futureStates, pastStates } = useMyStore.temporal.getState();

  return (
    <div>
      <h1>
        {" "}
        <span role="img" aria-label="bear">
          🐻
        </span>{" "}
        <span role="img" aria-label="recycle">
          ♻️
        </span>{" "}
        Zundo!
      </h1>
      past states: {JSON.stringify(pastStates)}
      <br />
      future states: {JSON.stringify(futureStates)}
      <br />
      current state: {JSON.stringify(store)}
      <br />
      <br />
      bears: {JSON.stringify(bears)}
      <br />
      <button onClick={increment}>increment</button>
      <button onClick={decrement}>decrement</button>
      <br />
      <button onClick={() => undo()}>undo</button>
      <button onClick={() => redo()}>redo</button>
      <button onClick={() => clear()}>clear</button>
    </div>
  );
};

export default App;
charkour commented 7 months ago

HI @tmanager2025,

Sorry for the confusion! You'll need to use the useStore from zustand to make the temporal selector reactive. Maybe I can update the docs to avoid confusion.

Add this to your <App /> function:

const { undo, redo, futureStates, pastStates, clear } = useStore(
    useMyStore.temporal,
 );

Here's a sandbox: https://codesandbox.io/p/sandbox/zundo-forked-l9dl2s

tmanager2025 commented 7 months ago

Sorry can't open the sandbox. Giving below message.

[Back to the dashboard](https://codesandbox.io/dashboard)
Sandbox not found
It's likely that the Sandbox you're trying to access doesn't exist or you don't have the required permissions to access it.
If the issue persists, please contact our support team at [support@codesandbox.io](mailto:support@codesandbox.io) for further assistance, making sure to copy the error details below.

Problem details and configurations

Review GitHub permissions

Show details
tmanager2025 commented 7 months ago

Ok I am able to work. Thank you so much 😊

For anyone looking below is updated sandbox.

https://codesandbox.io/p/sandbox/zundo-forked-3sjgk9?file=%2Fsrc%2FApp.tsx%3A17%2C27