Vuex: [2.0 feature request] Automatic store getters?

Created on 12 Aug 2016  路  4Comments  路  Source: vuejs/vuex

If vuex can support automatic store getters, instead of writing:

new Vuex.Store({
  state: {foo,bar,baz },
  getters: {
    foo: state => state.foo,
    bar: state => state.bar,
    baz: state => state.baz
  }
})

we can just assume that they will be created automatically

new Vuex.Store({
  state: {foo,bar,baz }
  // automatic store getters
})

Currently, I'm using a stateToGetters to make getters

const state = {foo,bar,baz},
new Vuex.Store({
    state,
    getters: {
      someOtherGetter: state => ...,
      ...stateToGetters(state)
    }
})
function stateToGetters(state) {
    return Object.keys(state).reduce((newObj, key) => {
        newObj[key] = state => state[key];
        return newObj;
    }, {});
}

So if automatic store getters is too implicit, maybe we can add an official stateToGetters(or whatever name) helper function like mapGetters and mapActions?

All 4 comments

There is mapState.

If I understood correctly, mapState is used inside component's computed options, and requires passing a function for each key, which doesn't address to this problem.
I'm currently using the same set of getters across multiple components, so using mapState at each one's computed options is much more verbose (requires passing one function for each key) than just generating getters on the store's side and using mapGetters.

Ah, okay. I think a custom helper is good enough for this case, as you get more control over how you want to do the mapping.

Okay, will just use custom helper functions :smile:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jbruni picture jbruni  路  3Comments

gdelazzari picture gdelazzari  路  3Comments

james-wasson picture james-wasson  路  3Comments

jdittrich picture jdittrich  路  3Comments

haoxins picture haoxins  路  4Comments