-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathzustand-client.ts
More file actions
36 lines (33 loc) · 857 Bytes
/
zustand-client.ts
File metadata and controls
36 lines (33 loc) · 857 Bytes
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
import { EventClient } from '@tanstack/devtools-event-client'
import { createStore } from 'zustand'
interface ZustandEventMap {
'zustand:stateChange': any
'zustand:revertSnapshot': any
}
export const eventClient = new EventClient<ZustandEventMap>({
pluginId: 'zustand',
})
export const store = createStore<{
count: number
increment: () => void
decrement: () => void
}>((set) => ({
count: 0,
increment: () => {
return set((state) => {
eventClient.emit('stateChange', { count: state.count + 1 })
return { count: state.count + 1 }
})
},
decrement: () => {
return set((state) => {
eventClient.emit('stateChange', { count: state.count - 1 })
return { count: state.count - 1 }
})
},
}))
eventClient.on('revertSnapshot', (snapshot) => {
store.setState({
count: snapshot.payload.count,
})
})