-
Notifications
You must be signed in to change notification settings - Fork 0
/
CounterStore.ts
37 lines (29 loc) · 915 Bytes
/
CounterStore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { createStore } from "zustand/vanilla"
// https://docs.pmnd.rs/zustand/guides/nextjs
type CounterState = {
count: number
}
type CounterActions = {
decrementCount: () => void
incrementCount: () => void
reset: () => void
}
export type CounterStore = CounterState & CounterActions
const defaultInitState: CounterState = {
count: 0,
}
export const initCounterStore = (): CounterState => {
return defaultInitState
}
// NOTE: use createStore instead of create when context is used
// https://tuffstuff9.hashnode.dev/zustand-create-vs-createstore
export const createCounterStore = (
initState: CounterState = defaultInitState,
) => {
return createStore<CounterStore>()((set) => ({
...initState,
decrementCount: () => set((state) => ({ count: state.count - 1 })),
incrementCount: () => set((state) => ({ count: state.count + 1 })),
reset: () => set(defaultInitState),
}))
}