Skip to content

Because after running undo my main store becomes undefined #200

@nappalm

Description

@nappalm

Store:

export const useBudgetStore = createStore(
  temporal<BudgetStore>(
    (set, get) => ({
      data: null,
      isLoading: false,
      error: null,
      onPushBudgetItem: (item: BudgetDataItem) => {
        set((state) => {
          if (!state.data) return state;
          return {
            data: {
              ...state.data,
              data: [...state.data.data, item],
            },
          };
        });
      },
      fetchData: async (date: string) => {
        set({ isLoading: true, error: null });

        const { data, error } = await getByDate(date);
        if (!error) {
          set({ data, isLoading: false });
          return;
        }

        set({ error: error.message, isLoading: false });
      },
    })
    },
  ),
);

export const useTemporalStore = <T extends unknown>(
  selector: (state: TemporalState<BudgetStore>) => T,
  equality?: (a: T, b: T) => boolean,
) => useStore(useBudgetStore.temporal, selector, equality);

Component:

  const store = useBudgetStore();
  const { undo, redo, pastStates, futureStates } = useTemporalStore();

  const handleAddRow = () => {
    const item = newBudgetItem();
    store.onPushBudgetItem(item);
    setEditable([item.id, "budget"]);
  };

My application works fine without using undo. However, I am adding items using the handleAddRow function which modifies my state. When I call the undo function, my store becomes undefined. Am I missing something to configure?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions