vuejs
/

Vue Pinia

Last Sync: Today

On this page

10
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

vuejs

Vue Pinia

What is Pinia?

Pinia is the official state management library for Vue, designed to replace Vuex with a simpler and more modern API.

Why Use Pinia?

  • Simple and intuitive API
  • Better TypeScript support
  • Lightweight and fast
  • DevTools integration

Install Pinia

BASHRead-only
1
npm install pinia

Setup Pinia

JavaScriptRead-only
1
import { createApp } from 'vue';
import { createPinia } from 'pinia';

const app = createApp(App);
app.use(createPinia());
app.mount('#app');

Create Store

JavaScriptRead-only
1
import { defineStore } from 'pinia';

export const useStore = defineStore('main', {
  state: () => ({ count: 0 }),
  actions: {
    increment() {
      this.count++;
    }
  },
  getters: {
    doubleCount: (state) => state.count * 2
  }
});

Using Store

JavaScriptRead-only
1
const store = useStore();
store.increment();
console.log(store.count);

Core Concepts

ConceptDescription
StateReactive data
ActionsMethods to modify state
GettersComputed values from state

Best Practices

  • Keep stores modular
  • Use actions for logic
  • Use getters for computed data
  • Avoid direct state mutation outside store

Common Mistakes

  • Putting too much logic in components
  • Not organizing stores properly
  • Overusing global state
  • Not using getters effectively

Conclusion

Pinia simplifies state management in Vue with a clean and modern approach, making applications easier to maintain and scale.

Try it yourself

const store = useStore();
store.increment();

Test Your Knowledge

Q1
of 3

Pinia used for?

A
Routing
B
State management
C
Styling
D
API
Q2
of 3

Store created using?

A
createStore
B
defineStore
C
useStore
D
makeStore
Q3
of 3

Getter returns?

A
State
B
Method
C
Computed value
D
API

Frequently Asked Questions

What is Pinia?

A modern state management library for Vue.

Is Pinia better than Vuex?

Yes, it is simpler and recommended.

What are getters?

Computed values based on state.

Previous

vue state management

Next

vue global state

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.