Created
August 20, 2024 23:02
-
-
Save JamesIde/aa7e701f1b201e85d9855f6879211688 to your computer and use it in GitHub Desktop.
Zustand TypeScript Slices Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://zustand.docs.pmnd.rs/guides/typescript#slices-pattern | |
export interface UserStore { | |
username: string; | |
setUsername: (username: string) => void; | |
} | |
export interface PreferencesStore { | |
darkMode: boolean; | |
} | |
const userSlice: StateCreator<UserStore> = (set) => ({ | |
username: "", | |
setUsername: (username) => set((s) => ({ ...s, username })), | |
}); | |
const preferenceSlice: StateCreator<PreferencesStore> = (set) => ({ | |
darkMode: false, | |
}); | |
export interface AppStore extends UserStore, PreferencesStore {} | |
export const appStore = create<AppStore>()((...a) => ({ | |
...userSlice(...a), | |
...preferenceSlice(...a), | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment