State — reactive data. Getters — computed properties.
1import { defineStore } from "pinia"23export const useCartStore = defineStore("cart", {4 state: () => ({5 items: [],6 discount: 07 }),8 getters: {9 total: (state) => {10 return state.items.reduce((sum, item) => {11 return sum + item.price * item.quantity12 }, 0)13 },14 finalTotal(state) {15 return this.total * (1 - this.discount)16 }17 },18 actions: {19 addItem(item) {20 this.items.push(item)21 }22 }23})
Key differences: