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.
- action or shared data
- reactive store
- subscribed component
Build a Pinia store
Core syntax or pattern
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
- action or shared data
import { 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.
- reactive store
actions: {Manage state shared across many components properly using a Pinia store.
- subscribed component
})Apply State Management with Pinia in a small example and test normal, edge, and failure cases.
- action or shared data
- reactive store
- 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.
import { defineStore } from 'pinia'
export const useCartStore = defineStore('cart', {
state: () => ({ items: [] }),
getters: {
totalItems: (state) => state.items.length
},
actions: {
add(product) {
this.items.push(product)
}
}
})cart.totalItems = 0Try 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
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
Quick quiz 2 / 3
Quick quiz 3 / 3
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