pmndrs / zustand

🐻 Bear necessities for state management in React

Home Page:https://zustand-demo.pmnd.rs/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cant user Array of Map type to store data

saketverma-03 opened this issue · comments

import { Collection, CollectionItem, ContainerNode } from "./types";
import { create } from "zustand";

import { devtools } from "zustand/middleware";

type State = {
  collection: Collection;
  user: string;
};

type Actions = {
  addContainerNode: () => void;
  removeContainerNode: (idx: number) => void;
};

const n = new Map<string, ContainerNode>();
n.set("lol", {
  title: "new",
  content: "new",
});
const initialCollection = [n];

export const useCollectionStore = create<State & Actions>()(
  devtools((set) => ({
    user: "dasked",
    collection: initialCollection,
    addContainerNode: () =>
      set((state) => {
        const temp = [...state.collection, n];
        return { ...state, collection: temp };
      }),

    removeContainerNode: (idx) =>
      set((s) => {
        const temp = s.collection.filter((_, i) => i != idx);
        return { ...s, collection: temp };
      }),
  })),
);

the collection is empty object in state , but it should be some sort of map !
i'm not sure how to get the behavior i want
image

i want to store array of Map in my collection in store.