Adding and using getters in your Pinia store
As stated earlier, getters in Pinia act just like computed properties. They allow you to request a simple value that’s generated by custom logic written in a function.
If you go back to the original Pinia store created by default, you’ll see it had a getter defined:
import { defineStore } from 'pinia' export const useCounterStore = defineStore({ id: 'counter', state: () => ({ counter: 0 }), getters: { doubleCount: (state) => state.counter * 2 }, // rest of file... })
The doubleCount
getter simply takes the current value of counter
and returns the double of it. As demonstrated, getters are automatically passed the current state as an argument, which can then be used in whatever logic makes sense in your particular getter function.
Just like regular values defined in the...