Master Pinia Setup Stores in 30 Minutes with Vue JS 3

Posted by


In this tutorial, we will walk through the process of setting up stores in Pinia, a state management library for Vue JS 3. By the end of this tutorial, you will have a clear understanding of how to set up stores in Pinia and how to use them in your Vue application.

Step 1: Install Pinia

The first step is to install Pinia in your Vue project. You can do this by running the following command in your terminal:

npm install pinia@next

Step 2: Create a Pinia store

To create a store in Pinia, you need to define a store object using the createStore function. Create a new file called store.js in your project and add the following code:

import { createPinia } from 'pinia'

export const store = createPinia()

Step 3: Register the store in your Vue app

To use the store in your Vue app, you need to register it in the root component. Open your main Vue file (usually main.js or App.vue) and add the following code:

import { createApp } from 'vue'
import App from './App.vue'
import { store } from './store'

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

Step 4: Create modules in the store

Now that you have set up the store, you can create modules to organize your state. Modules in Pinia are similar to modules in Vuex. In your store.js file, you can define a module like this:

import { defineStore } from 'pinia'

export const useCounterStore = defineStore({
  id: 'counter',
  state: () => ({
    count: 0
  }),
  actions: {
    increment() {
      this.count++
    },
    decrement() {
      this.count--
    }
  }
})

Step 5: Use the store in your components

To use the store in your components, you need to import the useStore function from Pinia and call it with the store module you want to use. For example, to use the counter module we just created, you can do this in a component:

import { defineComponent } from 'vue'
import { useCounterStore } from '@/store'

export default defineComponent({
  setup() {
    const counterStore = useCounterStore()

    return {
      counter: counterStore
    }
  }
})

Step 6: Test the store

You can now test the store in your Vue components by accessing the state and actions defined in the store module. For example, you can display the count value and increment it in a component like this:

<template>
  <div>
    <p>Count: {{ counter.count }}</p>
    <button @click="counter.increment">Increment</button>
    <button @click="counter.decrement">Decrement</button>
  </div>
</template>

<script>
import { defineComponent } from 'vue'
import { useCounterStore } from '@/store'

export default defineComponent({
  setup() {
    const counterStore = useCounterStore()

    return {
      counter: counterStore
    }
  }
})
</script>

That’s it! You have successfully set up stores in Pinia and used them in your Vue application. Pinia provides a simple and efficient way to manage state in Vue JS 3, making it easier to build scalable and maintainable applications. Happy coding!

0 0 votes
Article Rating

Leave a Reply

12 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@MakeAppswithDanny
2 hours ago

Thanks for watching!
👉 Pinia Setup Course (6 modules FREE): https://makeappsacademy.com
👉 Source Code: https://makeappsacademy.com/p/resources
👉 Pinia (Option Store) Tutorial: https://www.youtube.com/watch?v=JGC7aAC-3y8
👉 Pinia Option vs Setup Store Tutorial: https://www.youtube.com/watch?v=-HNmkKhrevE

@ikhwanullabib
2 hours ago

Came for the thumbnail, stay for the tutorial

@cuttingfilmsHD
2 hours ago

my fav channel about vue, thank u

@NourEldeenSayedHamdy
2 hours ago

You summarized it very very well,
Thank you

@Alaetouba
2 hours ago

wonderful explanation

@TecSanento
2 hours ago

Does it work with objects as well? And (how) does the type hinting work?

@dfordemo981
2 hours ago

I love your tutorials | super awesome and super helpful

@sharyuahire2572
2 hours ago

Hii, can you please make video on SSR in quasar

@mikaeltenshio8352
2 hours ago

Hello bro can i drop some question about capacitor?

@m8h4mm4d
2 hours ago

10 min ago I watch same video, but options api…
I think u duplicate ur video(2 years ago) xD

GJ Like Composition API!

@iUmerFarooq
2 hours ago

You explanation is amazing.
Thanks for your time and effort.
Waiting for more vuejs projects like vuejs with vuefire(firebase)

@CrazyPandaTuts
2 hours ago

thx Make.
best regards

12
0
Would love your thoughts, please comment.x
()
x