Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Handling key removal/addition/rename #397

Open
palfaro91 opened this issue Feb 27, 2021 · 1 comment
Open

Handling key removal/addition/rename #397

palfaro91 opened this issue Feb 27, 2021 · 1 comment

Comments

@palfaro91
Copy link

  • vuex-persistedstate version: 4.0.0-beta.3
  • node version: 13.12
  • npm (or yarn) version: 6.14.4

Relevant code or config

What you did:
I renamed a key in my state
What happened:
App crashed because the updated key didn't exist in localstorage state

Reproduction sandbox:

Problem description:
I renamed one key in my state definition and since the state was loaded from local storage the new key was not in the state. What's the proper way of updating a key name? for example my state definition originally looked like this

export const state  = () => ({
  foo: {
    user: {
      first_name: '',
    }
  }
})

so my local storage looked like { foo: {user: { first_name: '' } } }

i then updated my state definition to

export const state  = () => ({
  foo: {
    account: {
      first_name: '',
    }
  }
})

and references to foo.user.first_name were updated to foo.account.first_name. My app crashes here because state still has foo.user. How do i go about updating my local storage state so that it has the new key?
Suggested solution:

@Harm-Nullix
Copy link

You could use something like getState and then transforming it yourself:

getState(storage) {
    if (storage.foo?.user) {
        storage.foo.account = Object.assign({}, storage.foo.user)
        delete storage.foo.user
    }
   return storage
}

(not tested)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants