[Vuejs]-Mapstate to dynamic state objects of vuex store from the component

0👍

I have found an alternative way by using getters. When we are using that we don’t have to map state at all.

I am returning a function from getters with an argument since Vuex getters don’t accept arguments.

  symbol: (state) => (symbol) => {
           return state.payload[symbol]
       }

and from the component, I am watching for that getter.

   this.$store.watch(
          (state, getters) => getters.allSymbols(currency),
          (newValue, oldValue) => {
            this.symbolObjects[symbol] = newValue;
           // do something
          },
      );

the watch is triggering every time the currency update. Also using the Vuex watcher gives the benefit of accessing both old and new values.

Leave a comment