Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vuex store doesn't update component

Tags:

I'm new to vue, so I'm probably making a rookie error.

I have a root vue element - raptor.js:

const Component = { el: '#app', store, data: {     productList: store.state.productlist }, beforeCreate: function () {     return store.dispatch('getProductList', 'getTrendingBrands'); }, updated: function (){     console.log(111);     startSlider(); } }; const vm = new Vue(Component); 

Using this template

<div class="grid-module-single popular-products" id="app"> <div class="row">     <div class="popular-items-slick col-xs-12">         <div v-for="product in productList">             ...         </div>     </div> </div> 

My store is very simple store/index.js:

import Vue from 'vue'; import Vuex from 'vuex'; import model from '../../utilities/model';  Vue.use(Vuex);  export default new Vuex.Store({ state: {     productlist: [] }, mutations: {     setProductList(state, data) {         state.productlist = data;     } }, actions: {     getProductList({ commit }, action) {         return model.products().then(data => commit('setProductList', data));     } } }); 

In my vuex devtool, I can see, that the store is being updated https://www.screencast.com/t/UGbw7JyHS3

but my component is not being updated: https://www.screencast.com/t/KhXQrePEd

Question:

I can see from the devtools, that my code is working. The store is being updated with data. My component is not being updated,however. I thought it was enough just to add this in the data property on the component:

data: {     productList: store.state.productlist } 

but apparently the data object doesn't seem to be automatically synced with the store. So either I'm doing a complete vue no-no somewhere, or I need to tweak the code a bit. Anyway can anyone help me in the right direction.

Thanks a lot.

like image 967
Martin Kure Avatar asked Feb 12 '17 20:02

Martin Kure


People also ask

How do I update my Vuex state?

Vuex stores are reactive. When Vue components retrieve state from it, they will reactively and efficiently update if the store's state changes. You cannot directly mutate the store's state. The only way to change a store's state is by explicitly committing mutations.

Does Vue automatically update?

One of Vue's best features is reactivity. You change a variable, and Vue automatically updates what is rendered to the page. However, not everything in Vue is reactive by default.

What is the use of Mapstate in Vuex?

Mapping in Vuex enables you to bind any of the state's properties, like getters, mutations, actions, or state, to a computed property in a component and use data directly from the state. Although we can get the job done with this. $store.state.user.data.name , we can use a map helper to simplify it to this.

How do I Rerender components in Vue?

The best way to force Vue to re-render a component is to set a :key on the component. When you need the component to be re-rendered, you just change the value of the key and Vue will re-render the component.


2 Answers

UPDATE

Figured it out myself. Just had to replace the components data part with a computed method:

data:

data: {   productList: store.state.productlist } 

and replace it with.

computed: {     productList () {         return store.state.productlist;     } }, 
like image 153
Martin Kure Avatar answered Sep 24 '22 09:09

Martin Kure


data only work once on component before render, so you can use computed instead. like above answer, or you can use mapstate

import {mapState} from 'vuex' ... computed: mapState({   productList: state => state.productList })              
like image 28
masongzhi Avatar answered Sep 26 '22 09:09

masongzhi