Zustand perform action on a store created with init props

365 views Asked by At

Following the Zustand docs here, I have created a Zustand store with initial props.

export const useNameStore = (initProps?: Partial<NameStoreProps>) =>
  create<NameStore>()(
    subscribeWithSelector((set) => ({
      ...defaultProps,
      ...initProps,
      updateName: (name) => set(() => ({ name })),
    })),
  );

Inside another component I utilize the store in this way

 const [name, updateName] = useNameStore({ name: "init" })((state) => [
    state.name,
    state.updateName,
  ]);

The issue is when I call the updateName action, it does not updates the name with the new value.

I tried removing the initProps logic from the store and everything works fine.

export const useNameStore =
  create<NameStore>()(
    subscribeWithSelector((set) => ({
      ...defaultProps,
      ...initProps,
      updateName: (name) => set(() => ({ name })),
    })),
  );

The complete link to the First issue sandbox is here.

0

There are 0 answers