What is Pinia and how does it compare to Vuex?
Pinia is the current official state management solution for Vue 3. It was designed alongside the Composition API and is the recommended replacement for Vuex in all new projects.
| Vuex 4 | Pinia | |
|---|---|---|
| Mutations | Required (boilerplate) | None — mutate state directly |
| TypeScript | Poor — requires workarounds | Excellent out of the box |
| Structure | state / mutations / actions / getters | state / actions / getters only |
| Modules | Nested, complex | Flat — each store is independent |
| DevTools | Good | Better (time-travel, hot-reload) |
// Verbose — mutation required to change state
const store = createStore({
state: () => ({ count: 0 }),
mutations: { increment(state) { state.count++ } },
actions: { increment({ commit }) { commit('increment') } },
getters: { double: (state) => state.count * 2 },
})
store.dispatch('increment')
// Clean — no mutations layer
export const useCounterStore = defineStore('counter', () => {
const count = ref(0)
const double = computed(() => count.value * 2)
function increment() { count.value++ }
return { count, double, increment }
})
const store = useCounterStore()
store.increment() // direct action call
store.count++ // or mutate state directly
All Comments