[Vuejs]-Vuex Store value is changing while updating the reference

0👍

For time being i solved the issue. I was passing an object from the store and hence was updating the value. So i changed the code a little bit.

    let startdate = this.$store.getters.getDateRange.start
    let endDate = this.$store.getters.getDateRange.end     
    let compareStart = new Date(startdate.getUTCFullYear(), startdate.getUTCMonth(), startdate.getUTCDate())
compareStart.setDate(startdate.getDate() - 7)
    let compareEnd = new Date(endDate.getUTCFullYear(), endDate.getUTCMonth(), endDate.getUTCDate())
compareEnd.setDate(endDate.getDate() + 7)

I’m not sure is this the best way or not.

-1👍

Great Question! This is a classic Javascript case of Pass by Reference and Pass by Value.

In your case the startDate/endDate becomes a reference of this.$store.getters.getDateRange.start / end and thus whenever you update the startDate/endDate it changes the one at the store.

Looks to me your store variable is a JS date object. Try the following.

//Let's Breakdown References

let startdate = new Date(this.$store.getters.getDateRange.start.valueOf())
let endDate = new Date(this.$store.getters.getDateRange.end.valueOf())

Learn More about JS variables Here [https://hackernoon.com/javascript-reference-and-copy-variables-b0103074fdf0]

Leave a comment