I know this is a noob question but I can't figure it out despite looking through different posts. Please help.
I'm building a shopping cart with Vue and Vuex. I store the content of the cart in sessionStorage so that it persists if the user refreshes.
When the page loads, I am able to retrieve the cart from session and add it to vuex state but it doesn't reflect in the DOM until I add a new item to the cart. How can I solve this?
This is my code:
I check to see if there's any session in beforeUpdate:
beforeUpdate() {
// this fires twice. why?
let cart = this.store_slug + '_cart'
found_cart = JSON.parse(sessionStorage.getItem(cart))
if (found_cart) {
this.$store.commit('restoreCart', found_cart)
}
},
This is my 'restoreCart' mutation:
restoreCart(state, found_cart) {
state.cart.push(found_cart)
},
state.cart = newObjectdoes not have any effect ?state.cart = newObjecti.e.state.cart = found_carthas an effect. I can see in the vuejs browser extension that the item is in the cart but when I navigate to my cart, the items are not displayed. However, if I add a new item to the cart, both the new and old items are then displayed.