Pinia

Intuitive, type safe, light and flexible Store for Vue using the compositio...

README

Pinia logo


npm package build status code coverage



Pinia


Intuitive, type safe and flexible Store for Vue


- 💡 Intuitive
- 🔑 Type Safe
- ⚙️ Devtools support
- 🔌 Extensible
- 🏗 Modular by design
- 📦 Extremely light

Pinia works with both Vue 2 and Vue 3.

Pinia is the most similar English pronunciation of the word _pineapple_ in Spanish: _piña_. A pineapple is in reality a group of individual flowers that join together to create a multiple fruit. Similar to stores, each one is born individually, but they are all connected at the end. It's also a delicious tropical fruit indigenous to South America.



Help me keep working on this project 💚



Gold Sponsors

VueJobs

Silver Sponsors

VueMastery Prefect

Bronze Sponsors

Stanislas Ormières Antony Konstantinidis Storyblok NuxtJS



FAQ


A few notes about the project and possible questions:

Q: _Is Pinia the successor of Vuex?_

A: Yes

Q: _What about dynamic modules?_

A: Dynamic modules are not type safe, so instead we allow creating different stores that can be imported anywhere

Installation


  1. ``` sh
  2. # or pnpm or yarn
  3. npm install pinia
  4. ```

If you are using Vue <2.7, make sure to install latest @vue/composition-api:

  1. ``` sh
  2. npm install pinia @vue/composition-api
  3. ```

Usage


Install the plugin


Create a pinia (the root store) and pass it to app:

  1. ``` js
  2. // Vue 3
  3. import { createApp } from 'vue'
  4. import { createPinia } from 'pinia'
  5. import App from './App.vue'

  6. const pinia = createPinia()
  7. const app = createApp(App)

  8. app.use(pinia)
  9. app.mount('#app')
  10. ```

  1. ``` js
  2. // Vue 2
  3. import { createPinia, PiniaVuePlugin } from 'pinia'

  4. Vue.use(PiniaVuePlugin)
  5. const pinia = createPinia()

  6. new Vue({
  7.   el: '#app',
  8.   // other options...
  9.   // ...
  10.   // note the same `pinia` instance can be used across multiple Vue apps on
  11.   // the same page
  12.   pinia,
  13. })
  14. ```

Create a Store


You can create as many stores as you want, and they should each exist in different files:

  1. ```ts
  2. import { defineStore } from 'pinia'

  3. // main is the name of the store. It is unique across your application
  4. // and will appear in devtools
  5. export const useMainStore = defineStore('main', {
  6.   // a function that returns a fresh state
  7.   state: () => ({
  8.     counter: 0,
  9.     name: 'Eduardo',
  10.   }),
  11.   // optional getters
  12.   getters: {
  13.     // getters receive the state as first parameter
  14.     doubleCounter: (state) => state.counter * 2,
  15.     // use getters in other getters
  16.     doubleCounterPlusOne(): number {
  17.       return this.doubleCounter + 1
  18.     },
  19.   },
  20.   // optional actions
  21.   actions: {
  22.     reset() {
  23.       // `this` is the store instance
  24.       this.counter = 0
  25.     },
  26.   },
  27. })
  28. ```

defineStore returns a function that has to be called to get access to the store:

  1. ```ts
  2. import { useMainStore } from '@/stores/main'
  3. import { storeToRefs } from 'pinia'

  4. export default defineComponent({
  5.   setup() {
  6.     const main = useMainStore()

  7.     // extract specific store properties
  8.     const { counter, doubleCounter } = storeToRefs(main)

  9.     return {
  10.       // gives access to the whole store in the template
  11.       main,
  12.       // gives access only to specific state or getter
  13.       counter,
  14.       doubleCounter,
  15.     }
  16.   },
  17. })
  18. ```

Documentation


To learn more about Pinia, check its documentation.

License