Skip to content

Is there a way to set expiry time when using persist? #807

Closed Answered by AnatoleLucet
SeongwoonHong asked this question in Q&A
Discussion options

You must be logged in to vote

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:

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 () => {

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@SeongwoonHong
Comment options

@AnatoleLucet
Comment options

@bozzhik
Comment options

@dai-shi
Comment options

Answer selected by SeongwoonHong
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
middleware/persist This issue is about the persist middleware
4 participants