I
I
ivankirshin2018-06-01 22:52:22
Vue.js
ivankirshin, 2018-06-01 22:52:22

How to work with vuex correctly?

Today I realized that I'm a fool who has been working with a view for some time and did not know that an object received through a getter can be changed directly. It turns out that I often made a commit, although the data had already been changed.
As I understand it, this is not desirable. I read that you need to turn on street mode.
Now this code throws an error. As you can see, before I didn't know that puzzles and so changes after a variety. Now the question is different. How to do it right. What if objects and arrays are passed by reference? do another copy operation?

shuffle({commit,getters}){
        let puzzles = getters.getPuzzles
        puzzles.sort((a,b) => Math.random() - 0.5)
        commit('createPuzzles',puzzles)
        commit('resetCounter')
        commit('changeResult', false)
      }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-06-01
@ivankirshin

Now the question is different. How to do it right. What if objects and arrays are passed by reference? do another copy operation?

Yes.
let puzzles = getters.getPuzzles
Do instead
let puzzles = [...getters.getPuzzles]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question