Replies: 1 comment 3 replies
-
The typings of export function mapStores<Stores extends any[]>(
...stores: [...Stores]
): _Spread<Stores> {
return stores.reduce((reduced, useStore) => {
// @ts-expect-error: $id is added by defineStore
reduced[useStore.$id + mapStoreSuffix] = function (
this: ComponentPublicInstance
) {
return useStore(this.$pinia)
}
return reduced
}, {} as _Spread<Stores>)
} I think one can refactor this to cater to their personal preferences because I feel like adding an extra API to handle names will either be verbose by being more explicit: mapStores([useDefaultStore, { store: useOtherStore, name: 'customName' }) or too implicit and easy to mess up: mapStores([useDefaultStore, [useOtherStore, 'customName'])
// or was it ['customName', useOtherStore]? |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Current
mapStores
implementation spreads passed stores ids in the component'scomputed
field. This behavior might be inconvenient in case of dynamic store id.In my case store id is usually may be formed like here:
or even
This allows better store control when the same widget is reused on the page but it needs to store different data.
The issue is - passing stores with complex ids to
mapStores
results in complexcomputed
properties and it is painful to access them, e.g. if I do something like this:Accessing
auth
gonna bethis.mainScreeen_authManager
which is ridiculous.As a workaround we split such stores into separated
computed
properties:Which works like a charm but results in some "boilerplate" code when I need to register three and more stores in such a manner.
As a proposed API I think the best posible variant is to allow passing an array instead of useStore function in
mapStores
, where the fisrt element is the desired name:Looks like this won't break anything and is not hard to implement.
Do someone encountered this problem? Is there a chance to something like the mentioned solution would be implemented in the
pinia
codebase?Beta Was this translation helpful? Give feedback.
All reactions