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?
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: