Home > OS >  How to get the whole pinia state object?
How to get the whole pinia state object?

Time:10-07

I'm trying to have a getter that return all state properties of my pinia store.

export const useFilterStore = defineStore('filterStore', {
    state : () : FilterState => ({
        variables:[] as string[],
        categories:[] as string[]
    }),


    getters: {
        getFilters: (state) => state
    },
    actions : {}
  });

Doing it this way , getFilters references itself generating circular reference on different situation, as illustrated in this component

import { useFilterStore } from './store/filter-store.js'
export default {
  name: 'App',

  setup() {
    const filter = useFilterStore()
    JSON.stringify(filter.getFilters)  --> cyclic object value
  }
}

Is there a way to set up a getter for the whole state object directly?

CodePudding user response:

getFilters doesn't serve a good purpose. state is the whole store, not just state data, accessing state.getFilters results in a recursion.

It should be:

 JSON.stringify(filter.$state)
  • Related