robinvdvleuten / vuex-persistedstate

💾 Persist and rehydrate your Vuex state between page reloads.
https://npm.im/vuex-persistedstate
MIT License
5.76k stars 375 forks source link

Wrong data after restore. #431

Open papaIOprog opened 2 years ago

papaIOprog commented 2 years ago

Relevant code or config:

export const state = () => ({
  colorScheme: {
      base: {"bg-color-primary": true},
      light: {"bg-color-primary-light": true},
      dark: {"bg-color-primary-dark": true}
    }
})

export const mutations = {
  changeColor(state, colorName) {
    state.colorScheme.base = {"bg-color-orange": true};
    state.colorScheme.light = {"bg-color-orange-light": true};
    state.colorScheme.dark = {"bg-color-orange-dark": true}
  }
}

What you did:

What happened:

{
  "base": {
    "bg-color-primary": true,
    "bg-color-orange": true
  },
  "light": {
    "bg-color-primary-light": true,
    "bg-color-orange-light": true
  },
  "dark": {
    "bg-color-primary-dark": true,
    "bg-color-orange-dark": true
  }
}
// Following data was in localstorage before reload
{
  "base": {
    "bg-color-orange": true
  },
  "light": {
    "bg-color-orange-light": true
  },
  "dark": {
    "bg-color-orange-dark": true
  }
}

Problem description:

Harm-Nullix commented 2 years ago

May I say:

U are using a mutation to do three updates, consider using an action

Next of, u are setting a default state, that state will always be used on start, and if you have updates, things will get merged. If they were not, a lot of other use cases would fail.

Is your localstorage updated with the default, or does it live in you vuex object? In any way, you could use sometinhg like the overwrite attribute of the API. Or look for another option, if the "error" is in the plugin, you could find the fix for your case there.