Search code examples
javascriptvue.jsvuex

Vuex - transform store object without altering original


I'm trying to copy a store object for local transformation but my alteration function is changing the original store object also. I'm using ...mapState["storeObject"]

This is basically what's happening:

state.storeOjb = "original value" 
this.local = storeOjb ;
this.local = "altered version of storeOjb"

storeOjb === "original value" // false -- why?

Solution

  • I'm not sure exactly what's your problem is. But you can remove the object mutation by

    this.local = Object.assign({}, storeObj)
    

    Vue

    In Vue JS, for local changes you can use computed property of the storeObj.

    computed: {
      localValue: function() {
        return { ...storeObj, newChange: true }
      }
    }