Home » vue » State mutated by getter

State mutated by getter

Posted by: admin November 26, 2021 Leave a comment

Questions:

I’m trying to write a getter that returns a simple number from state, decremented by 1

const getters = {
  getCurrentView: state => {
    return types.PAGES_OBJECT[state.currentViewNum]
  },
  getCurrentViewNum: state => {
    return state.currentViewNum--
  }
};

However this actually mutates the state.

I have tried assigning it to a var, but it appears that var becomes a direct reference to state.

How do I do this, without mutating state?

Answers:

Isn’t x-- equivalent to x = x - 1?

The -- operator is mutating your object.

return state.currentViewNum - 1 instead