Skip to content

Instantly share code, notes, and snippets.

@livemehere
Created March 24, 2024 08:44
Show Gist options
  • Save livemehere/c58d400d030911406728da49a8c087d1 to your computer and use it in GitHub Desktop.
Save livemehere/c58d400d030911406728da49a8c087d1 to your computer and use it in GitHub Desktop.
zustand persist boiler template
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