Four slice patterns #2647
Unanswered
dai-shi
asked this question in
Show and tell
Replies: 2 comments
-
Simplified Bear Zustandimport { create, StateCreator } from "zustand";
interface CountSlice {
count: number;
incCount: () => void;
resetCount: () => void;
}
interface TextSlice {
text: string;
updateText: (newText: string) => void;
resetText: () => void;
}
interface SharedSlice {
reset: () => void;
}
const createCountSlice: StateCreator<CountSlice> = (set) => ({
count: 0,
incCount: () => set((state) => ({ count: state.count + 1 })),
resetCount: () => set({ count: 0 }),
});
const createTextSlice: StateCreator<TextSlice> = (set) => ({
text: "Hello",
updateText: (text) => set({ text }),
resetText: () => set({ text: "Hello" }),
});
const useCounterStore = create<CountSlice & TextSlice & SharedSlice>()(
(set, get, api) => ({
...createCountSlice(set, get, api),
...createTextSlice(set, get, api),
reset: () => {
get().resetCount();
get().resetText();
},
}),
); |
Beta Was this translation helpful? Give feedback.
0 replies
-
Poll: https://x.com/dai_shi/status/1812649338654617918 Similar discussion: #2672 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Bare (Bear) Zustand
https://tsplay.dev/NaAE6N
Zustand Lens
https://tsplay.dev/Wz1GEm
Zustand Slices
https://tsplay.dev/mxOkXN
Zustand Valtio
https://tsplay.dev/wQdRAW
Beta Was this translation helpful? Give feedback.
All reactions