Replies: 5 comments 20 replies
-
I know that you can spin up multiple instances of Vuex modules. I'm wondering if this can be achived in Pinia to solve the exact situation your describing. |
Beta Was this translation helpful? Give feedback.
-
maybe this can help you? pinia-di. |
Beta Was this translation helpful? Give feedback.
-
Don't know if this still applies, but I found a way to do it like you say
export const useTableStore = (tableId: string) => defineStore(`tables/${tableId}`, {
state: () => ({
foo: {},
bar: {}
}),
getters: ...,
actions: ...,
})(); // <- this is the key right here, by initialising it here Then in other component or components: const tableId = props.table.id // Just as an example
const tableStore = useTableStore(tableId) Assuming the tableIds are different for each table, they will use a new instance of this store, so they won't share state unless the tableId is the same |
Beta Was this translation helpful? Give feedback.
-
I'm not sure if I'm the only one who understood the problem differently or not. |
Beta Was this translation helpful? Give feedback.
-
Apparently, you can use internal component property |
Beta Was this translation helpful? Give feedback.
-
I'll try to explain the idea by describing my use case. Maybe, there is already a better solution for it.
Imagine you need to implement something like a datagrid - a searchable, sortable, filterable and pageable table of some data. This is a complex component composed of many sub-components. Naturally, you want to separate the state management part into... a store. This would keep the current search and sort criteria, found results and maybe some state related to the appearance and behavior of the datagrid parts. It would also have actions to call search API based on the current state. Such store would allow all sub-components to access the data and to modify it (e.g. trigger search) in a controlled and uniform way.
The problem is that all the store libraries like Vuex and Pinia provide global central stores, which are tied to the application root. However, there are valid use cases when several datagrids are displayed on a single page (e.g. some kind data comparison). The state of each datagrid component tree should be independent from others and its lifecycle should be bound to the lifecycle of its root component.
Can this be achieved with Pinia?
Beta Was this translation helpful? Give feedback.
All reactions