Created
July 25, 2019 04:27
-
-
Save stwilz/8bcba580cc5b927d7993cddb5dfb4cb1 to your computer and use it in GitHub Desktop.
A curry utility for passing additional arguments to `mapGetters`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { mapGetters } from "vuex"; | |
const curryMapGetters = args => (namespace, getters) => | |
Object.entries(mapGetters(namespace, getters)).reduce( | |
(acc, [getter, fn]) => ({ | |
...acc, | |
[getter]: state => | |
fn.call(state)(...(Array.isArray(args) ? args : [args])) | |
}), | |
{} | |
); |
Rewrote a bit to make it more readable (https://gist.github.com/bekliev/5fbd348054d2f57bc0b0af0720b9cfff):
import { mapGetters } from 'vuex';
const curryMapGetters = (...args) => (namespace, getters) => {
const getters = mapGetters(namespace, getters);
const entries = Object.entries(getters);
const initialAccumulator = {};
return entries.reduce((acc, [getterName, getterHandler]) => {
acc[getterName] = (state) => {
return getterHandler.call(state)(...args);
};
return acc;
}, initialAccumulator);
};
Did you make this work properly? I borrowed your idea with slight modifications, however, the problem is I need to access the store in the child component as well, and if I pass the LISTS_TYPE
as a prop listsType
to the media-table component, then the curryMapGetters
function runs before this
is defined, and I receive an error: Cannot read properties of undefined (reading 'listsType').
I'm a little stumped.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
great! thank you