A
A
Androbim2019-05-15 13:46:26
Vue.js
Androbim, 2019-05-15 13:46:26

What is the Unexpected side effect in computed property error?

Good day!
Here is such a problem.
5cdbec9b223d2112341660.png
Here is the code:

data() {
    return {
      messagesforuser:[],
      newmessagesforchat: []
    };
  },
computed: {
  ...
    messagesforuserReverse: function() {
       return this.messagesforuser.reverse()
    }
  },

I think the point is that it is initially empty, but what to do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-05-15
@Androbim

You shouldn't change the data in computed properties, but you do - because the reverse method does not create a new array, but changes the original one. So before using it, the array should be copied:
return [...this.messagesforuser].reverse()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question