vuexjs getter with argument

Javascriptvue.jsVuejs2Vuex

Javascript Problem Overview


Is there a way to pass parameter into getter of vuex store? Something like:

new Vuex.Store({
  getters: {
    someMethod(arg){
       // return data from store with query on args
    }
  }
})

So that in component I could use

<template>
    <div>
        <p>{{someMethod(this.id)}}</p>
    </div>
</template>
<script lang="ts">
    import { mapGetters } from "vuex"

    export default {
        props: ['id'],
        computed: mapGetters(['someMethod'])
        }
    }
</script>

but in vuex first argument is state and second is other getters. Is it possible?

Javascript Solutions


Solution 1 - Javascript

One way to do this can be:

new Vuex.Store({
  getters: {
    someMethod(state){
      var self = this;
       return function (args) {
          // return data from store with query on args and self as this
       };       
    }
  }
})

However, getter does not take arguments and why is explained in this thread:

>the naming convention is slightly confusing, getters indicates state can be retrieved in any form, but in fact they are reducers. > >Perhaps we should have reducers being pure methods. Which can be used for filtering, mapping ect. > >getters then can be given any context. Similar to computed, but you can now combine computed props to getters in vuex option. Which helps structure of components.

Edit:

A better way to achieve the same thing will be using ES6 arrow as detailed out in the answer of nivram80, using method style getters where you can pass a parameter by returning a function form the getter:

new Vuex.Store({
  getters: {
    someMethod: (state) => (id) => {
        return state.things.find(thing => thing.id === id)
      }
    };       
  }
})

Solution 2 - Javascript

An ES6 arrow function would work nicely here too. For example, sake, let's say you're looking for a particular 'thing' in your store.

new Vuex.Store({
  getters: {
    someMethod: (state) => (id) => {
      return state.things.find(thing => thing.id === id)
    }
  },
})

Here is another example via the Vuex documentation

Solution 3 - Javascript

You can pass arguments to getters by returning a function. This is particularly useful when you want to query an array in the store:

getters: {
  // ...
  getTodoById: (state) => (id) => {
    return state.todos.find(todo => todo.id === id)
  }
}

Inside your vue component:

store.getters.getTodoById(2) // -> { id: 2, text: '...', done: false }

Note that getters accessed via methods will run each time you call them, and the result is not cached.

The above answer is fetched from official Vue docs: https://vuex.vuejs.org/guide/getters.html#method-style-access

Solution 4 - Javascript

You can use the MapGetters helper like this, once define store getters:

new Vuex.Store({
  getters: {
    someMethod(state){
       return (value) => {
          return value;
       }
    }
  }
})

Then call getter from a component like this:

<script>
    import { mapGetters } from "vuex"

    export default {
     computed: {
     ...mapGetters(['someMethod'])
     },
     mounted() {
       console.log(this.someMethod('hello world')); // then logs "hello world"
     }       
}
</script>

Solution 5 - Javascript

I don't think that this is what a vuex.getter is meant to do.

first, as you can see in all the above examples the getter can be mapped as a computed only

<script>
    import { mapGetters } from "vuex"

    export default {
     computed: {
     ...mapGetters(['someGetter'])
     },
     mounted() {
       console.log(this.someGetter); // a computed is not a method.
     }       
}
</script>

and if you need it to receive arguments, what you want is a method, not a computed.

I would recommend you to used an store.action instead:

new Vuex.Store({
  actions: {
    someMethod({ state }, arg){
       // do something with state.someValue and the arg
       return someTransformationOfYourState;
    }
  }
})

as actions and mutations can be mapped as methods. and you can use it like this:

<script>
    import { mapActions } from "vuex"

    export default {
     computed: {
     
     },
     mounted() {
       // now you can use someMethod with args in your component 
       this.someMethod('the arg');
     },
     methods: {
     ...mapActions(['someMethod'])
     },       
}
</script>

the first argument of an action is the store itself, so you can access the state from there. same as the dispatch and commit functions.

NOTE that an action can only recieve one paramether (payload) so if you need to send more, you will have to wrap all of them in an object or array

this.someMethod({ arg1, arg2, ...});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSlyView Question on Stackoverflow
Solution 1 - JavascriptSaurabhView Answer on Stackoverflow
Solution 2 - Javascriptnivram80View Answer on Stackoverflow
Solution 3 - JavascriptPriyank View Answer on Stackoverflow
Solution 4 - JavascriptAlper GuvenView Answer on Stackoverflow
Solution 5 - JavascriptJonosView Answer on Stackoverflow