Vuex - Computed property based on a modules state does not update on dispatch?












1














I am exploring the module documentation for Vuex and have been stuck for quite a while now with updating a value within a modules state from the component which uses it. Here is what I have so far:



app/store/index.js



import Vue from 'vue'
import Vuex from 'vuex'
import Counter from './modules/Counter'
Vue.use(Vuex)
export default new Vuex.Store({
modules: { Counter }
})


app/store/modules/Counter.js



const state = {
current: 0
}

const mutations = {
INCREMENT_CURRENT_COUNT (state) {
state.main++
}
}

const actions = {
increment ({ commit }) {
commit('INCREMENT_CURRENT_COUNT')
}
}

export default {
namespaced: true,
state,
mutations,
actions
}


app/components/Test.vue



<template>
<div id="wrapper">
<p>{{ count }}</p>
<button @click="something()">Do something</button>
<button @click="add()">Add to count</button>
</div>
</template>

<script>
import { mapState, mapActions } from 'vuex'

export default {
computed: mapState({
count: state => state.Counter.current
}),
methods: {
something () {
alert('hello')
},
...mapActions('Counter', {
add: 'increment'
}),
}
}
</script>


Essentially, all I am trying to do is increment the current value in the Counter module when clicking the button in the component which fires the add() method, it is what I might expect given the above but what actually happens is nothing.



So no error and the count value within the vue component remains at 0.



Any ideas on what what am I doing wrong here?










share|improve this question




















  • 1




    Can you replicate this issue on Codesandbox? It'd make it easier to debug and understand it.
    – Phiter
    Nov 19 '18 at 12:23










  • @Phiter Thanks for the input. codesandbox.io/s/415j60717 and it works (facepalm). My use case and issue was a little more complicated though as I had explained in a comment on the answer.
    – Craig van Tonder
    Nov 19 '18 at 13:28










  • damn I didn't even notice this when i was reading the question
    – Phiter
    Nov 19 '18 at 13:33
















1














I am exploring the module documentation for Vuex and have been stuck for quite a while now with updating a value within a modules state from the component which uses it. Here is what I have so far:



app/store/index.js



import Vue from 'vue'
import Vuex from 'vuex'
import Counter from './modules/Counter'
Vue.use(Vuex)
export default new Vuex.Store({
modules: { Counter }
})


app/store/modules/Counter.js



const state = {
current: 0
}

const mutations = {
INCREMENT_CURRENT_COUNT (state) {
state.main++
}
}

const actions = {
increment ({ commit }) {
commit('INCREMENT_CURRENT_COUNT')
}
}

export default {
namespaced: true,
state,
mutations,
actions
}


app/components/Test.vue



<template>
<div id="wrapper">
<p>{{ count }}</p>
<button @click="something()">Do something</button>
<button @click="add()">Add to count</button>
</div>
</template>

<script>
import { mapState, mapActions } from 'vuex'

export default {
computed: mapState({
count: state => state.Counter.current
}),
methods: {
something () {
alert('hello')
},
...mapActions('Counter', {
add: 'increment'
}),
}
}
</script>


Essentially, all I am trying to do is increment the current value in the Counter module when clicking the button in the component which fires the add() method, it is what I might expect given the above but what actually happens is nothing.



So no error and the count value within the vue component remains at 0.



Any ideas on what what am I doing wrong here?










share|improve this question




















  • 1




    Can you replicate this issue on Codesandbox? It'd make it easier to debug and understand it.
    – Phiter
    Nov 19 '18 at 12:23










  • @Phiter Thanks for the input. codesandbox.io/s/415j60717 and it works (facepalm). My use case and issue was a little more complicated though as I had explained in a comment on the answer.
    – Craig van Tonder
    Nov 19 '18 at 13:28










  • damn I didn't even notice this when i was reading the question
    – Phiter
    Nov 19 '18 at 13:33














1












1








1







I am exploring the module documentation for Vuex and have been stuck for quite a while now with updating a value within a modules state from the component which uses it. Here is what I have so far:



app/store/index.js



import Vue from 'vue'
import Vuex from 'vuex'
import Counter from './modules/Counter'
Vue.use(Vuex)
export default new Vuex.Store({
modules: { Counter }
})


app/store/modules/Counter.js



const state = {
current: 0
}

const mutations = {
INCREMENT_CURRENT_COUNT (state) {
state.main++
}
}

const actions = {
increment ({ commit }) {
commit('INCREMENT_CURRENT_COUNT')
}
}

export default {
namespaced: true,
state,
mutations,
actions
}


app/components/Test.vue



<template>
<div id="wrapper">
<p>{{ count }}</p>
<button @click="something()">Do something</button>
<button @click="add()">Add to count</button>
</div>
</template>

<script>
import { mapState, mapActions } from 'vuex'

export default {
computed: mapState({
count: state => state.Counter.current
}),
methods: {
something () {
alert('hello')
},
...mapActions('Counter', {
add: 'increment'
}),
}
}
</script>


Essentially, all I am trying to do is increment the current value in the Counter module when clicking the button in the component which fires the add() method, it is what I might expect given the above but what actually happens is nothing.



So no error and the count value within the vue component remains at 0.



Any ideas on what what am I doing wrong here?










share|improve this question















I am exploring the module documentation for Vuex and have been stuck for quite a while now with updating a value within a modules state from the component which uses it. Here is what I have so far:



app/store/index.js



import Vue from 'vue'
import Vuex from 'vuex'
import Counter from './modules/Counter'
Vue.use(Vuex)
export default new Vuex.Store({
modules: { Counter }
})


app/store/modules/Counter.js



const state = {
current: 0
}

const mutations = {
INCREMENT_CURRENT_COUNT (state) {
state.main++
}
}

const actions = {
increment ({ commit }) {
commit('INCREMENT_CURRENT_COUNT')
}
}

export default {
namespaced: true,
state,
mutations,
actions
}


app/components/Test.vue



<template>
<div id="wrapper">
<p>{{ count }}</p>
<button @click="something()">Do something</button>
<button @click="add()">Add to count</button>
</div>
</template>

<script>
import { mapState, mapActions } from 'vuex'

export default {
computed: mapState({
count: state => state.Counter.current
}),
methods: {
something () {
alert('hello')
},
...mapActions('Counter', {
add: 'increment'
}),
}
}
</script>


Essentially, all I am trying to do is increment the current value in the Counter module when clicking the button in the component which fires the add() method, it is what I might expect given the above but what actually happens is nothing.



So no error and the count value within the vue component remains at 0.



Any ideas on what what am I doing wrong here?







javascript vuejs2 vuex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 13:26









Sajib Khan

10.5k42942




10.5k42942










asked Nov 19 '18 at 12:21









Craig van Tonder

3,429114982




3,429114982








  • 1




    Can you replicate this issue on Codesandbox? It'd make it easier to debug and understand it.
    – Phiter
    Nov 19 '18 at 12:23










  • @Phiter Thanks for the input. codesandbox.io/s/415j60717 and it works (facepalm). My use case and issue was a little more complicated though as I had explained in a comment on the answer.
    – Craig van Tonder
    Nov 19 '18 at 13:28










  • damn I didn't even notice this when i was reading the question
    – Phiter
    Nov 19 '18 at 13:33














  • 1




    Can you replicate this issue on Codesandbox? It'd make it easier to debug and understand it.
    – Phiter
    Nov 19 '18 at 12:23










  • @Phiter Thanks for the input. codesandbox.io/s/415j60717 and it works (facepalm). My use case and issue was a little more complicated though as I had explained in a comment on the answer.
    – Craig van Tonder
    Nov 19 '18 at 13:28










  • damn I didn't even notice this when i was reading the question
    – Phiter
    Nov 19 '18 at 13:33








1




1




Can you replicate this issue on Codesandbox? It'd make it easier to debug and understand it.
– Phiter
Nov 19 '18 at 12:23




Can you replicate this issue on Codesandbox? It'd make it easier to debug and understand it.
– Phiter
Nov 19 '18 at 12:23












@Phiter Thanks for the input. codesandbox.io/s/415j60717 and it works (facepalm). My use case and issue was a little more complicated though as I had explained in a comment on the answer.
– Craig van Tonder
Nov 19 '18 at 13:28




@Phiter Thanks for the input. codesandbox.io/s/415j60717 and it works (facepalm). My use case and issue was a little more complicated though as I had explained in a comment on the answer.
– Craig van Tonder
Nov 19 '18 at 13:28












damn I didn't even notice this when i was reading the question
– Phiter
Nov 19 '18 at 13:33




damn I didn't even notice this when i was reading the question
– Phiter
Nov 19 '18 at 13:33












2 Answers
2






active

oldest

votes


















2














You should change state.main++ to state.current++






const mutations = {
INCREMENT_CURRENT_COUNT (state) {
// change state.main -> state.current
state.current++
}
}








share|improve this answer























  • Godammit I didn't notice that
    – Phiter
    Nov 19 '18 at 12:35










  • Well spotted, thank you! This being said I did spot this when I was making an example on codesandbox. The issue for me specifically is that I was using this in conjuction with electron, specifically vuex-electron and did not note the part where it says you need to create an instance of store in the main process. Solved my problem with that although I know it was not really included in my simple example (no tags for this package yet).
    – Craig van Tonder
    Nov 19 '18 at 13:25



















0














The problem isn't with the click handler or the action, it's in the mutation.
In your mutation INCREMENT_CURRENT_COUNT you have this:



INCREMENT_CURRENT_COUNT (state) {
state.main++
}


Whilst your state only has a property called current.



In order to make it work, change:



state.main++


To:



state.current++





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374525%2fvuex-computed-property-based-on-a-modules-state-does-not-update-on-dispatch%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    You should change state.main++ to state.current++






    const mutations = {
    INCREMENT_CURRENT_COUNT (state) {
    // change state.main -> state.current
    state.current++
    }
    }








    share|improve this answer























    • Godammit I didn't notice that
      – Phiter
      Nov 19 '18 at 12:35










    • Well spotted, thank you! This being said I did spot this when I was making an example on codesandbox. The issue for me specifically is that I was using this in conjuction with electron, specifically vuex-electron and did not note the part where it says you need to create an instance of store in the main process. Solved my problem with that although I know it was not really included in my simple example (no tags for this package yet).
      – Craig van Tonder
      Nov 19 '18 at 13:25
















    2














    You should change state.main++ to state.current++






    const mutations = {
    INCREMENT_CURRENT_COUNT (state) {
    // change state.main -> state.current
    state.current++
    }
    }








    share|improve this answer























    • Godammit I didn't notice that
      – Phiter
      Nov 19 '18 at 12:35










    • Well spotted, thank you! This being said I did spot this when I was making an example on codesandbox. The issue for me specifically is that I was using this in conjuction with electron, specifically vuex-electron and did not note the part where it says you need to create an instance of store in the main process. Solved my problem with that although I know it was not really included in my simple example (no tags for this package yet).
      – Craig van Tonder
      Nov 19 '18 at 13:25














    2












    2








    2






    You should change state.main++ to state.current++






    const mutations = {
    INCREMENT_CURRENT_COUNT (state) {
    // change state.main -> state.current
    state.current++
    }
    }








    share|improve this answer














    You should change state.main++ to state.current++






    const mutations = {
    INCREMENT_CURRENT_COUNT (state) {
    // change state.main -> state.current
    state.current++
    }
    }








    const mutations = {
    INCREMENT_CURRENT_COUNT (state) {
    // change state.main -> state.current
    state.current++
    }
    }





    const mutations = {
    INCREMENT_CURRENT_COUNT (state) {
    // change state.main -> state.current
    state.current++
    }
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 19 '18 at 12:36

























    answered Nov 19 '18 at 12:34









    Sajib Khan

    10.5k42942




    10.5k42942












    • Godammit I didn't notice that
      – Phiter
      Nov 19 '18 at 12:35










    • Well spotted, thank you! This being said I did spot this when I was making an example on codesandbox. The issue for me specifically is that I was using this in conjuction with electron, specifically vuex-electron and did not note the part where it says you need to create an instance of store in the main process. Solved my problem with that although I know it was not really included in my simple example (no tags for this package yet).
      – Craig van Tonder
      Nov 19 '18 at 13:25


















    • Godammit I didn't notice that
      – Phiter
      Nov 19 '18 at 12:35










    • Well spotted, thank you! This being said I did spot this when I was making an example on codesandbox. The issue for me specifically is that I was using this in conjuction with electron, specifically vuex-electron and did not note the part where it says you need to create an instance of store in the main process. Solved my problem with that although I know it was not really included in my simple example (no tags for this package yet).
      – Craig van Tonder
      Nov 19 '18 at 13:25
















    Godammit I didn't notice that
    – Phiter
    Nov 19 '18 at 12:35




    Godammit I didn't notice that
    – Phiter
    Nov 19 '18 at 12:35












    Well spotted, thank you! This being said I did spot this when I was making an example on codesandbox. The issue for me specifically is that I was using this in conjuction with electron, specifically vuex-electron and did not note the part where it says you need to create an instance of store in the main process. Solved my problem with that although I know it was not really included in my simple example (no tags for this package yet).
    – Craig van Tonder
    Nov 19 '18 at 13:25




    Well spotted, thank you! This being said I did spot this when I was making an example on codesandbox. The issue for me specifically is that I was using this in conjuction with electron, specifically vuex-electron and did not note the part where it says you need to create an instance of store in the main process. Solved my problem with that although I know it was not really included in my simple example (no tags for this package yet).
    – Craig van Tonder
    Nov 19 '18 at 13:25













    0














    The problem isn't with the click handler or the action, it's in the mutation.
    In your mutation INCREMENT_CURRENT_COUNT you have this:



    INCREMENT_CURRENT_COUNT (state) {
    state.main++
    }


    Whilst your state only has a property called current.



    In order to make it work, change:



    state.main++


    To:



    state.current++





    share|improve this answer


























      0














      The problem isn't with the click handler or the action, it's in the mutation.
      In your mutation INCREMENT_CURRENT_COUNT you have this:



      INCREMENT_CURRENT_COUNT (state) {
      state.main++
      }


      Whilst your state only has a property called current.



      In order to make it work, change:



      state.main++


      To:



      state.current++





      share|improve this answer
























        0












        0








        0






        The problem isn't with the click handler or the action, it's in the mutation.
        In your mutation INCREMENT_CURRENT_COUNT you have this:



        INCREMENT_CURRENT_COUNT (state) {
        state.main++
        }


        Whilst your state only has a property called current.



        In order to make it work, change:



        state.main++


        To:



        state.current++





        share|improve this answer












        The problem isn't with the click handler or the action, it's in the mutation.
        In your mutation INCREMENT_CURRENT_COUNT you have this:



        INCREMENT_CURRENT_COUNT (state) {
        state.main++
        }


        Whilst your state only has a property called current.



        In order to make it work, change:



        state.main++


        To:



        state.current++






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 12:34









        T. Dirks

        1,019318




        1,019318






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374525%2fvuex-computed-property-based-on-a-modules-state-does-not-update-on-dispatch%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            How to fix TextFormField cause rebuild widget in Flutter