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

I can't update state with new data from Tanstack Query

devoren opened this issue · comments

Summary

Hey! Thank you for awesome library ❤️
I have a form. When I update the form, onSuccess returns the new updated data. I don't understand why my state isn't updating. I don't know why its not updating, because i have the same logic inside useEffect and it works

Link to reproduction

updateStore(
  {
    id: store.id,
    ...data
  },
  {
    onSuccess: (data) => {
    // if i set store like this, not working
    setStore(data.response.ressult.store)
     
    // if i set store like this, works
    setStore({
        ...store,
        ...data,
      });

      Notice.Success(data.result.message);
    },
  }
);

Store

interface StoresState {
  store: IStore | null;
  setStore: (store: IStore | null) => void;
}

export const useStoresStore = create<StoresState>()(
  persist(
    (set) => ({
      store: null,
      setStore: (store) => set(() => ({ store })),
      clearStore: () => set({ store: null }),
    }),
    {
      name: "stores-zsg",
    }
  )
);

Check List

Please do not ask questions in issues.

  • I've already opened a discussion before opening this issue, or already discussed in other media.

Please include a minimal reproduction.

Please check this if you're filing an issue regarding TypeScript.

  • I've read the typescript guide, in particular that create is to be used as create<T>()(...) and not create<T>(...).

Any solution mate? ran into same problem. State not updating

Any solution mate? ran into same problem. State not updating

Edit:: My mistake. data was inside another data :P got it fixed