Created
March 24, 2024 08:44
-
-
Save livemehere/c58d400d030911406728da49a8c087d1 to your computer and use it in GitHub Desktop.
zustand persist boiler template
This file contains 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
import { create } from "zustand"; | |
import { createJSONStorage, devtools, persist } from "zustand/middleware"; | |
import { immer } from "zustand/middleware/immer"; | |
type State = { | |
lastAccessTime: number; | |
}; | |
type Actions = { | |
setLastAccessTime: (timestamp: number) => void; | |
reset: () => void; | |
}; | |
const initialState: State = { | |
lastAccessTime: 0, | |
}; | |
export const usePersistStore = create<State & Actions>()( | |
devtools( | |
persist( | |
immer((set) => ({ | |
...initialState, | |
reset: () => set(initialState, false, "reset"), | |
setLastAccessTime: (timestamp) => | |
set( | |
(state) => { | |
state.lastAccessTime = timestamp; | |
}, | |
false, | |
"setLastAccessTime", | |
), | |
})), | |
{ | |
name: "persistStore", | |
storage: createJSONStorage(() => localStorage), | |
}, | |
), | |
{ name: "persistStore" }, | |
), | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment