Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 38 ยท Getting Started

Pinia vs Vuex and Other State Options

Compare Pinia with Vuex, plain composables, provide/inject and the URL as state holders, see the same feature written four ways, and learn how to choose.

Vue has no single "one true way" to share state. You can lift a ref into a module, use provide/inject, write a composable, use the older Vuex library, or use Pinia. Each works, and each has a sweet spot. This lesson compares them with the same small feature, so you can decide with facts instead of fashion.

Where Pinia sits

Pinia began in 2019 as an experiment to redesign Vuex for the Composition API. It became so close to what the Vuex 5 proposal described that the Vue team adopted it as the official recommendation for Vue 3. Vuex is in maintenance mode: it still works and receives fixes, but it gets no new features. For new Vue 3 projects, use Pinia.

Question Vuex 4 Pinia
Mutations Required for every change None, change state directly or in actions
Modules Nested, with namespaces Each store is flat and independent
TypeScript Awkward, needs manual typing Types are inferred from the definition
Composition API Possible, with helper functions Native, plus setup stores
Boilerplate Higher Lower
DevTools Yes Yes, with time travel
Size About 10 KB About 1.5 KB core
Status Maintenance Official, actively developed

If you maintain a Vuex app, Migration from Vuex later in this course explains how to move gradually.

One feature, four ways

The feature: a counter shared by two components. Here is the same logic with each tool.

1. A module-level reactive object

The simplest shared state is a ref created outside any component:

JavaScript
// shared/counter.js
import { ref } from "vue";
export const count = ref(0);
export const increment = () => { count.value++; };

Any component that imports it shares it. This is perfect for small apps. Its limits: no DevTools, one instance per JavaScript module (a problem for SSR, where it would leak between users), and no standard place for structure. Building a Simple Store develops this approach.

2. A composable

A composable is a function that returns reactive state. If it creates the state inside the function, every caller gets a separate copy, which is what you want for reusable logic:

JavaScript
export function useCounter() {
  const count = ref(0);
  return { count, increment: () => count.value++ };
}

For sharing, composables are the wrong tool unless you hoist the state out of the function, which brings back the module singleton problem. They are excellent for logic that each component instance owns. See Composables.

3. Vuex

JavaScript
import { createStore } from "vuex";

const store = createStore({
  state: () => ({ count: 0 }),
  getters: { double: (state) => state.count * 2 },
  mutations: { increment(state) { state.count++; } },
  actions: { incrementLater({ commit }) { setTimeout(() => commit("increment"), 500); } },
});

// component
store.commit("increment");
store.dispatch("incrementLater");

Two concepts, commit for mutations and dispatch for actions, and strings as the API. It works but takes more ceremony.

4. Pinia

JavaScript
const useCounterStore = defineStore("counter", {
  state: () => ({ count: 0 }),
  getters: { double: (state) => state.count * 2 },
  actions: {
    increment() { this.count++; },
    incrementLater() { setTimeout(() => this.increment(), 500); },
  },
});

// component
counter.increment();
counter.incrementLater();

No mutations, no strings, and counter.increment autocompletes in your editor. Here is a runnable comparison of the approaches. The first button uses a hand-made shared ref, the second the real Pinia store.

Plain shared ref vs Pinia store

Both approaches share state across components. The difference appears as the app grows: Pinia gives structure, DevTools, SSR safety and plugins.

provide / inject and other options

  • provide / inject passes data down a subtree without props. It is great for component libraries (a tabs component sharing the active tab with its children). It is not a global store: data is scoped to the providing component's descendants, and there are no DevTools.
  • The URL is state, too. Filters, page numbers and search terms often belong in the query string so that links can be shared. Use the router for these and keep them out of Pinia.
  • Server data (lists fetched from an API) is a special case. Libraries such as TanStack Query for Vue handle caching and refetching better than a hand-written store. Pinia can still hold the UI state around it.
  • Browser storage persists data between visits. It is not a replacement for a store, but a store can be saved to it; persistence covers that.

A decision guide

flowchart TD
    A["Who needs the data?"] -->|"One component"| B["ref / reactive"]
    A -->|"Parent and children"| C["props, emits, provide/inject"]
    A -->|"Reusable logic, one copy per user"| D["Composable"]
    A -->|"Many unrelated components, one shared copy"| E["Pinia store"]
    A -->|"Can be shared as a link"| F["URL query via the router"]

Start with the narrowest tool that works and widen only when you feel the pain.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which concept from Vuex does Pinia drop?

    Pinia has no mutations. State is changed directly or inside actions.
  2. 2. What is the main risk of a module-level ref used as shared state in a server-rendered app?

    A module is evaluated once per server process. Pinia creates stores per app instance, so each request is isolated.
  3. 3. A composable that creates ref(0) inside its function gives each caller:

    State created inside the function body is new for every call, which is right for per-component logic and wrong for sharing.
  4. 4. Which belongs in the URL query rather than a store?

    State that should survive a page reload and be shareable as a link belongs in the URL.

Summary

  • Pinia is the official Vue 3 state library; Vuex is in maintenance mode.
  • Pinia removes mutations and namespaced modules and gives inferred types and DevTools.
  • A module-level ref shares state simply but has no structure, tooling or SSR safety.
  • Composables are for reusable, per-caller logic; Pinia is for one shared instance.
  • Pick the narrowest tool that solves the problem.

Next: DevTools and Hot Module Replacement