How to return to the previous state? (Vuex)

3

I'm using Vuex to manage the states of my app and I'm creating an advance button and a back one, in the case of my progress I use the function below:

addUser() {
      const payload = {
        name: this.name,
        email: this.email
      };
      this.$store.commit("CHANGE_USER", payload);
    }

But I was in doubt on the back button, should I commit an empty variable? Is it possible to go back to a "previous state"? Is this the right way?

    
asked by anonymous 20.08.2018 / 15:36

1 answer

0

One solution would be for you to move through the object and clear each key. Ex.:

  let obj = {
    name: "Exemplo 1",
    email: "[email protected]",
    extra: {
      picture: "imagem.jpg"
    }
  }

  function resetObject(obj){

      for(let i in obj){
        if( typeof obj[i] === "string" ){
           obj[i] = ""
        }
        else if(typeof obj[i] === "object"){
           obj[i] = resetObject( obj[i] )     
        }
      }

      return obj

  }

  console.log( resetObject(obj) )
    
27.09.2018 / 01:15