Is there a way to set expiry time when using persist? #807
-
I'm using the persist middleware to store data to localStorage. I'd like to purge the what's in localStorage every 5 minutes. how would I do that? |
Beta Was this translation helpful? Give feedback.
Answered by
AnatoleLucet
Feb 15, 2022
Replies: 1 comment 4 replies
-
The In case you do want to restore the default values, you could do something like the following: import create from "zustand"
import { persist } from "zustand/middleware"
const defaultState = {
foo: "bar"
}
export const useStore = create(persist(
(set) => ({
...defaultState,
reset: () => {
useStore.persist.clearStorage();
set(defaultState);
}
}),
{
...
}
))
// then somewhere in your app
const myStore = useStore()
useEffect(() => {
const intervalId = setInterval(() => {
myStore.reset()
}, 1000 * 60 * 5);
return () => {
clearInterval(intervalId)
}
}, []) |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
SeongwoonHong
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
useStore.persist.clearStorage()
option might help you, but it does not restore the zustand state to its default values.In case you do want to restore the default values, you could do something like the following: