Skip to content
Tutorials Center
My learning

Topics

Pinia and Vue State Management Tutorial

Learn Pinia and Vue State Management step by step: 38 lessons in 9 chapters with examples, quizzes, diagrams and projects.

Start learning →

of lessons completed

Getting Started

  1. 1 What Is Pinia? Learn what Pinia is, what problem a store solves, how shared state differs from local component state, and see a real Pinia store run live in the browser.
  2. 2 Installing and Setting Up Pinia Install Pinia with npm or create-vue, register createPinia with app.use, learn the recommended folder layout and how the live examples on this site load Pinia from a CDN.
  3. 3 Your First Store Build your first Pinia store step by step with defineStore, state, getters and actions, then use it in components and learn how the id and the use function work.
  4. 4 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.
  5. 5 DevTools and Hot Module Replacement Inspect Pinia stores in Vue DevTools, understand pinia.state, see how timeline events and time travel work, and enable hot module replacement with acceptHMRUpdate.

Stores and State

  1. 6 State Basics Learn how Pinia state works: define it as a function, read and write it from components, use arrays and objects, understand reactivity rules and add new state safely.
  2. 7 Destructuring Stores with storeToRefs Learn why destructuring a Pinia store breaks reactivity, how storeToRefs keeps state and getters reactive, and why actions are destructured directly from the store.
  3. 8 Patching and Resetting State Learn how to change several Pinia state values at once with $patch (object and function forms), replace state with $state, and restore the initial state with $reset.
  4. 9 Subscribing to State Changes Learn how to react to every change of a Pinia store with $subscribe, read the mutation type and payload, choose flush and detached options, and know when watch is better.
  5. 10 Typing State with TypeScript Type Pinia state with interfaces, as casts and return types, handle null and empty arrays, and see how the types flow to components. Includes a runnable JavaScript demo.
  6. 11 Organizing Stores Decide what belongs in a Pinia store and what does not. Learn naming, file layout, store size, normalized state and when to keep state local to a component instead.

Getters and Actions

  1. 12 Getters Learn Pinia getters: derive values from state, use this and other getters, pass arguments by returning a function, read other stores, and understand caching.
  2. 13 Actions Learn Pinia actions: methods with this, calling actions from components, composing actions, validation and early returns, and why actions beat scattered state writes.
  3. 14 Async Actions and API Calls Write async Pinia actions that call APIs, track loading and error state, avoid stale responses and double requests, and cache results. Runnable demos use a fake API.
  4. 15 Watching Actions with $onAction Use store.$onAction to run code before and after every action, read its name and arguments, handle success and errors, measure timing and write a logger or analytics hook.
  5. 16 Stores Using Other Stores Learn how a Pinia store can use another store in getters and actions, where to call the use function, how to avoid circular dependencies, and how to share logic safely.

Composition and Setup Stores

  1. 17 Setup Stores Learn the Pinia setup syntax: write a store like a Vue setup function with ref, computed and functions, what you must return, how $reset and private state behave, and when it fits.
  2. 18 Setup or Options: Choosing a Style Compare Pinia's setup and options store syntax side by side: readability, TypeScript, $reset, watchers, composables, testing and migration, with a clear rule for choosing.
  3. 19 Composables and Watchers in Setup Stores Use watch, watchEffect and your own composables inside Pinia setup stores: debounced search, derived saves, reusable logic, and the limits around inject, router and SSR.
  4. 20 Using Stores Outside Components Use Pinia stores in router guards, API helpers and plain modules: when the active pinia exists, how to pass the pinia instance explicitly, and the mistakes that cause errors.

Plugins and Persistence

  1. 21 Writing a Pinia Plugin Learn what a Pinia plugin is, when it runs, what it receives and how to write one that adds a property to every store, with runnable examples.
  2. 22 Extending Stores from Plugins Extend every Pinia store from a plugin: add new state, methods and non-reactive properties, hook into actions and state changes, and declare custom options.
  3. 23 Persisting Pinia State Across Reloads Save Pinia state so it survives a page reload: write a small persistence plugin with $subscribe, restore on creation, choose what to save and handle storage errors.
  4. 24 The persistedstate Plugin and Its Options Use pinia-plugin-persistedstate in a real project: install it, set persist options like key, pick, omit, storage and serializer, and test the same ideas live.

Testing Stores

  1. 25 Testing Pinia Stores with Vitest Learn to unit-test Pinia stores without a component: create a fresh pinia per test, call actions, assert state and getters, and test async actions and setup stores.
  2. 26 Testing Components with createTestingPinia Test Vue components that use Pinia: mount with createTestingPinia, stub actions with spies, set initialState, override getters and decide when to run real actions.
  3. 27 Mocking Actions and APIs in Pinia Tests Keep Pinia tests fast and reliable: inject the API client, mock modules with vi.mock, fake timers and responses, and test stores that call other stores.

SSR and Nuxt

  1. 28 Pinia with SSR and Hydration Understand how Pinia works with server-side rendering: one pinia per request, fetching data in stores, serializing state into the HTML and hydrating it in the browser.
  2. 29 Using Pinia in Nuxt Set up Pinia in a Nuxt app with the @pinia/nuxt module, auto-imported stores, fetching with callOnce and $fetch, route middleware and Nuxt-specific plugins.
  3. 30 SSR Pitfalls and Request State in Pinia Avoid the classic Pinia SSR bugs: state leaking between visitors, stores used outside setup, browser-only code on the server, non-serializable state and hydration mismatches.

Migration from Vuex

  1. 31 Vuex Concepts Mapped to Pinia Map every Vuex concept to its Pinia equivalent: state, getters, mutations, actions, modules, helpers and subscriptions, with the same app written in both libraries.
  2. 32 Migrating a Vuex Module to Pinia Step by Step Convert a real namespaced Vuex cart module into a Pinia store in clear steps, handle rootState and cross-module calls, and prove the behavior is identical.
  3. 33 Migrating Components and Running Vuex Beside Pinia Move a Vue app from Vuex to Pinia without a big-bang rewrite: install both, migrate leaf modules first, convert components with map helpers and bridge the two stores.

Projects and Practice

  1. 34 Project: A Todo App with Filters Build a complete todo app with Pinia: a setup store with state, getters and actions, filters, a derived counter, an action log and tests for the store itself.
  2. 35 Project: A Shopping Cart with Multiple Stores Build a shopping cart with three cooperating Pinia stores: a product catalog, a cart with coupons and totals, and a checkout flow with stock checks, errors and persistence.
  3. 36 Project: An Auth Store with Route Guards Build login and logout with a Pinia auth store: user state, roles, token handling, a route guard that redirects visitors, and a plugin that resets every store on logout.
  4. 37 Project: An Async Data Store with Caching Build a reusable Pinia store for server data: loading and error states per key, a time-based cache, de-duplicated requests, invalidation and optimistic updates with rollback.
  5. 38 Pinia Best Practices and Practice Exercises A practical Pinia checklist: how to organize stores, what belongs in state, getters and actions, the classic reactivity mistakes, and hands-on exercises with tests to fix.