How to return to the previous state? (Vuex)

Asked

Viewed 109 times

5

I am using Vuex to manage the states of my app and I am creating a forward button and a back, in case of my advance 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 to an empty variable? Is it possible to go back to a "previous state"? That’s the right way?

  • You can try to store the user’s current value in the component, if you click forward you commit it to the store and if it comes back you do nothing and leave the value that was already in the store. Works for you?

2 answers

0

If I understand correctly you can have two "commits" for each case. One recording the current_user and the other recording the last_user for example.

Ai is in charge of the actions of each button know which state they must look.

0

I believe that the best solution would be to create another state (Array[]) to store the changes in the user.

And in CHANGE_USER Mutation, you would . push in this state with the current user state, only after that you would update the user object.

And then on the other Mutation of recovering the user you would just update the user’s calling object. pop in the Array with the change history, so you would remove the last item and return it to its state.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.