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...