Getting Started with Pinia State Management in Vue 3 — Part 1

<miniScript/>
Geek Culture
Published in
3 min readMar 10, 2023

Pinia is a state management library for Vue 3 that provides a simple and powerful way to manage your application’s state. It’s built on top of the new Vue 3 reactivity API and provides a flexible and scalable architecture for managing state in your Vue 3 applications.

In this blog post, we will explore Pinia and see how it can help you manage your application state more efficiently. We will also take a look at some basic code examples to see how Pinia works.

Why Pinia?

Vue 3 introduces a new reactivity API that makes it easier to work with reactive data. While the new API is powerful, it can be challenging to manage complex state in large applications. This is where Pinia comes in. Pinia provides a simple and flexible architecture for managing state in your Vue 3 applications.

Pinia provides a store pattern that allows you to define a store for managing a specific piece of application state. This makes it easy to organize your application state and makes it easy to share state across components.

Getting Started

To use Pinia, you need to install it in your project. You can install it using npm or yarn via terminal.

npm install pinia

or

yarn add pinia

Once you have installed Pinia, you can create a store by defining a class that extends the Pinia Store class.

import { defineStore } from 'pinia'

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

In the example above, we define a store that manages a counter. The store has a state object that contains a count variable. We also define two actions, increment, and decrement, that modify the count variable.

To use the store in a component, we can import it and use the useStore hook to create an instance of the store.

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

<script>
import { useCounterStore } from './store'

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

return {
count: counterStore.count,
increment: counterStore.increment,
decrement: counterStore.decrement
}
}
}
</script>

In the example above, we use the useCounterStore hook to create an instance of the store. We then return the count variable and the increment and decrement actions from the setup function.

We can then use the count variable and the increment and decrement actions in the template.

Conclusion

Pinia is a powerful state management library for Vue 3 that provides a simple and flexible architecture for managing state in your applications. It’s built on top of the new Vue 3 reactivity API and provides a scalable and organized way to manage state in large applications. If you’re building a Vue 3 application, give Pinia a try!

… to be continued

Written with love ❤️ by miniScript on my desk while crafting e-commerce stores.
Linkedin
Twitter

--

--

<miniScript/>
Geek Culture

Coder, Lifestyle, Troubleshooter, Entrepreneur, Horse rider, GT Avalanche Rider