Thuta Learning
Vue.js
AdvancedWeb DevelopmentBeginner

State Management with Pinia

Vue.jsLesson 1825 min

What you'll walk away with

  • Build a Pinia store
  • Separate state, getters, and actions
  • Use a store inside a component

Explain it simply

State Management with Pinia, in plain language

Don't rush to move local component state elsewhere. Reach for Pinia when state is shared across multiple components — things like user sessions, a cart, or app-wide filters. Declare state as a function, write derived state as getters, and write changes as actions.

Problem → Why → Solution

The problem — why this concept matters

Not registering the Pinia plugin on the app

A common mistake

Dumping every bit of component-local state into a global store

Core concept — understand how it works

Manage state shared across many components properly using a Pinia store.

Mental model
  1. action or shared data
  2. reactive store
  3. subscribed component

Build a Pinia store

Core syntax or pattern

javascript
import { defineStore } from 'pinia'

export const useCartStore = defineStore('cart', {
  state: () => ({ items: [] }),
  getters: {
    totalItems: (state) => state.items.length
  },
  actions: {
    add(product) {
      this.items.push(product)
    }
  }
})

Manage state shared across many components properly using a Pinia store.

Read the code step by step

  1. action or shared dataimport { defineStore } from 'pinia'

    Don't rush to move local component state elsewhere. Reach for Pinia when state is shared across multiple components — things like user sessions, a cart, or app-wide filters. Declare state as a function, write derived state as getters, and write changes as actions.

  2. reactive storeactions: {

    Manage state shared across many components properly using a Pinia store.

  3. subscribed component})

    Apply State Management with Pinia in a small example and test normal, edge, and failure cases.

Execution flow
  1. action or shared data
  2. reactive store
  3. subscribed component

Success

Build a Pinia store

Failure

Not registering the Pinia plugin on the app

Let's break it down simply

Don't rush to move local component state elsewhere. Reach for Pinia when state is shared across multiple components — things like user sessions, a cart, or app-wide filters. Declare state as a function, write derived state as getters, and write changes as actions.

javascript
import { defineStore } from 'pinia'

export const useCartStore = defineStore('cart', {
  state: () => ({ items: [] }),
  getters: {
    totalItems: (state) => state.items.length
  },
  actions: {
    add(product) {
      this.items.push(product)
    }
  }
})
You should see
cart.totalItems = 0

Try it yourself

Build a Pinia store with theme and locale state for user preferences.

Pinia State — Pinia

Real-world usage

How to apply it in a real project

Apply State Management with Pinia in a small example and test normal, edge, and failure cases.

Mini challenge

Build it yourself

Build a Pinia store with theme and locale state for user preferences.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
javascript
import { defineStore } from 'pinia'

export const useCartStore = defineStore('cart', {
  state: () => ({ items: [] }),
  getters: {
    totalItems: (state) => state.items.length
  },
  actions: {
    add(product) {
      this.items.push(product)
    }
  }
})

Expected: Build a Pinia store

Show hint

Apply State Management with Pinia in a small example and test normal, edge, and failure cases.

Quick quiz 1 / 3

What best describes the core idea of State Management with Pinia?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • action or shared data → reactive store → subscribed component
  • Manage state shared across many components properly using a Pinia store.
  • Not registering the Pinia plugin on the app
  • Apply State Management with Pinia in a small example and test normal, edge, and failure cases.

Pinia State — Pinia

Easy traps

  • Not registering the Pinia plugin on the app
  • Dumping every bit of component-local state into a global store

Exercise

Build a Pinia store with theme and locale state for user preferences.

You'll know it worked when: cart.totalItems = 0

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

State Management with Pinia | Thuta Learning