Melkeydev / astrostation

https://astrostation.me
MIT License
216 stars 43 forks source link

Add a way to delete local storage on new deployments to avoid breaking changes #108

Closed Melkeydev closed 2 years ago

Melkeydev commented 2 years ago


const defaultState = {
  version: APP_VERSION,
  something: [],
  otherThing: true
}

const cachedState = JSON.parse(localStorage.getItem('state'))

let initialState = defaultState
if (cachedState?.version === APP_VERSION) {
  initialState = cachedState
} else {
  localStorage.setItem('state', JSON.stringify(initialState))
}

export const useStore = create(set => ({
  ...initialState,

  someMethod(thing) {
    set({ thing })
  }
}))```