Nuxt 3 and Pinia

Nuxt 3 and Pinia

Integrate Pinia as your state management library for your Nuxt 3 application.

Vuex -> Pinia

Evan You, the creator of Vue himself, has stated “Pinia is de facto Vuex 5! At this point it’s really a naming/br…

Integrate Pinia as your state management library for your Nuxt 3 application.



Nuxt 3 and Pinia

Integrate Pinia as your state management library for your Nuxt 3 application.



Vuex -> Pinia

Evan You, the creator of Vue himself, has stated “Pinia is de facto Vuex 5! At this point it’s really a naming/branding issue.”

For the time being, it’s probably best to be looking towards Pinia content rather than Vuex.

"Pinia is de facto Vuex 5! At this point it’s really a naming/branding issue."

I recommend reading VueJS’s official post regarding this to get a better understanding as to why Pinia > Vuex.



Installing Pinia in Nuxt 3

Pinia nearly comes with first-class support for Nuxt 3. You’ll need to install two packages:

yarn add pinia
yarn add @pinia/nuxt



Add Pinia to your nuxt.config file

You’ll need to add '@pinia/nuxt' to your buildModules array.

// nuxt.config.js
import { defineNuxtConfig } from 'nuxt3'

export default defineNuxtConfig({
  buildModules: ['@pinia/nuxt'],
})



Build your Pinia store

Now build a named store. For my use-case, I needed to manage state regarding filters, so the skeleton of my store looks like:

// store/filters.js
import { defineStore } from 'pinia'

export const useFiltersStore = defineStore({
  id: 'filter-store',
  state: () => {
    return {
      filtersList: ['youtube', 'twitch'],
    }
  },
  actions: {},
  getters: {
    filtersList: state => state.filtersList,
  },
})

This is just showing the general structure of your store. The key is to defineStore and make sure to include an id. In this case, I’m using 'filter-store' as my id but it could be anything you prefer.

Read over Pinia’s Docs to get a better grasp of how to use Pinia properly.



Bring Pinia in Vue Component

With our store in place, simply import it into the component you want to use it in and have fun!

<template>
  <div>
    {{ filtersList }}
  </div>
</template>

// components/FilterMenu.vue
<script>
import { useFiltersStore } from '~/store/filters'

export default defineComponent({
  setup() {
    const filtersStore = useFiltersStore()
    const filtersList = filtersStore.filtersList

    return { filtersList }
  },
})
</script>

Print Share Comment Cite Upload Translate
APA
Cody Bontecou | Sciencx (2024-03-28T21:26:34+00:00) » Nuxt 3 and Pinia. Retrieved from https://www.scien.cx/2022/02/17/nuxt-3-and-pinia/.
MLA
" » Nuxt 3 and Pinia." Cody Bontecou | Sciencx - Thursday February 17, 2022, https://www.scien.cx/2022/02/17/nuxt-3-and-pinia/
HARVARD
Cody Bontecou | Sciencx Thursday February 17, 2022 » Nuxt 3 and Pinia., viewed 2024-03-28T21:26:34+00:00,<https://www.scien.cx/2022/02/17/nuxt-3-and-pinia/>
VANCOUVER
Cody Bontecou | Sciencx - » Nuxt 3 and Pinia. [Internet]. [Accessed 2024-03-28T21:26:34+00:00]. Available from: https://www.scien.cx/2022/02/17/nuxt-3-and-pinia/
CHICAGO
" » Nuxt 3 and Pinia." Cody Bontecou | Sciencx - Accessed 2024-03-28T21:26:34+00:00. https://www.scien.cx/2022/02/17/nuxt-3-and-pinia/
IEEE
" » Nuxt 3 and Pinia." Cody Bontecou | Sciencx [Online]. Available: https://www.scien.cx/2022/02/17/nuxt-3-and-pinia/. [Accessed: 2024-03-28T21:26:34+00:00]
rf:citation
» Nuxt 3 and Pinia | Cody Bontecou | Sciencx | https://www.scien.cx/2022/02/17/nuxt-3-and-pinia/ | 2024-03-28T21:26:34+00:00
https://github.com/addpipe/simple-recorderjs-demo