My store as below
import { create } from "zustand";
type State = {
name: string;
age: number;
};
type Action = {
updateName: (name: State['name']) => void;
updateAge: (age: State['age']) => void
};
const initialState: State = {
name: 'bob',
age: 18,
};
export const useTempStore = create<State & Action>((set) => {
return {
...initialState,
updateName: (name) => set(() => ({ name })),
updateAge: (age) => set(() => ({ age })),
}
});
Now, I wanna persist store to server, so I changed the code as below
export const useTempStore = create<State & Action>((set) => {
return {
...initialState,
updateName: (name) => set(() => {
request(name);
return { name }
}),
updateAge: (age) => set(() => {
request(age);
return { age }
}),
}
});
const request = (data) => {
// Send XHR to server
};
But if I have a lot of state, I will have to call request
in every action. How can I make it call request
when very action execute?