Everything you need to know about Pinia in Vue.js

Everything you need to know about Pinia in Vue.js Picture 1

What is Pinia?

Pinia is a Vue-specific management library, designed to bring unparalleled simplicity and efficiency to Vue projects. Pinia is inspired by the best of modern management, and integrates into your application extremely light and simple.

The philosophy behind Pinia is to keep things to a minimum & tasteful, making it easy for developers to manage their apps. With an intuitive approach, Pinia lets you focus on the most common problems while delivering an exceptional user experience when building Vue apps.

Core Concepts of Pinia

  1. Getters are responsible for extracting and returning specific values ​​from the store. By defining getters you can efficiently access & process data without having to directly tweak the underlying.
  2. Actions play an important role in Pinia, allowing you to modify the state of the store by performing asynchronous or synchronous operations.
  3. The Store is the 'heart' of Pinia, encapsulating the app's state, getter, actions, and mutations.
  4. State refers to the data that the store manages.

 

What is Vuex?

Everything you need to know about Pinia in Vue.js Picture 2

Vuex is well known as a powerful solution for state management. So Pinia compared to Vuex, which option is more outstanding?

Pinia uses Vue's reactive system for state management, avoiding the need for any external dependencies. That means the Pinia ecosystem is more centralized and avoids potential long code bugs. Plus, the TypeScript support & tight typing it provides, allows you to catch bugs early in development, leading to more complete applications with fewer bugs.

If you're starting a new Vue project or considering a Vuex migration, Pinia is an attractive alternative that streamlines state management without sacrificing flexibility or performance.

Build a simple Vue app using Pinia

To better understand Pinia, try building a basic to-do list management app. Pinia provides the tools you need to manage the state for such applications.

Necessary conditions

First, prepare the required environment for this project, starting with Vue CLI.

# Để cài Vue CLI: npm install -g @vue/cli # Để tạo thư mục dự án với Vue CLI: vue create pinia-todo-app

At this stage, you can see in the terminal you need to select a preset. Continue by selecting Vue 3 from the default settings.

You can now install Pinia in the project directory:

cd pinia-todo-app npm install pinia

File Setup

You only need to edit a few files to complete this sample project. First, create a file named store.js in the src directory . This file will contain, add, and remove items you add to your to-do list. It will do all this using core Pinia concepts.

 

// src/store.js import { defineStore } from "pinia"; export const useTodoStore = defineStore("todo", { state: () => ({ todos: [], }), actions: { addTodo(todoText) { this.todos.push({ id: Date.now(), text: todoText }); }, removeTodo(todoId) { this.todos = this.todos.filter((todo) => todo.id !== todoId); }, }, });

But, of course, this file alone is not enough. You need to link the store.js file with App.vue . To do that, change the src/App.vue file as follows:

// src/App.vue 

The logic here is quite simple. The events you define in store.js will occur as you specify in the App.vue file . It's like the App.vue file will act as your control mechanism.

Of course, this way the app isn't everything because you don't use templates or styles. To add them, you can edit the App.vue file as follows:

// src/App.vue

My Pinia Todo List

  1. {{ todo.text }}

Now you have everything you need. Using the core concept features in Pinia, you can check the status, add & remove new items from the to-do list as you wish. But there is one last step. You should update the main.js file in the src directory and include the necessary directories for the application.

// src/main.js import { createApp } from "vue"; import { createPinia } from "pinia"; import App from "./App.vue"; const pinia = createPinia(); const app = createApp(App); app.use(pinia); app.mount("#app");

 

Everything needed to test the application is ready.

Run the sample Pinia app

To see the results of this simple but instructive to-do list, use the following command:

npm run serve

You'll see a clean, simple interface for adding and removing tasks:

Everything you need to know about Pinia in Vue.js Picture 3

Overall, Pinia is a valuable tool when you need a smooth and efficient data flow in your application. By providing a modular, secure, responsive architecture, Pinia simplifies state management and increases operational efficiency.

4.5 ★ | 2 Vote

May be interested

  • How to play Pokemon GO in Landscape Mode on the iPhoneHow to play Pokemon GO in Landscape Mode on the iPhone
    although players can play pokemon go in portrait mode. however, if you want to watch and play games on a large and eye-catching screen, players can switch to playing games in landscape mode.
  • The secret to controlling Pokemon Go employees at workThe secret to controlling Pokemon Go employees at work
    these days, hr managers are faced with an extremely painful problem that is the status of priority employees playing pokemon go more than work. this has caused a small impact on productivity and efficiency.
  • Check out the 'buffalo' Pokémon in Pokémon GoCheck out the 'buffalo' Pokémon in Pokémon Go
    each type of pokemon has hp, cp, ability to attack and endure differently. based on these indicators, players can determine as well as choosing the most powerful pokemon for their offensive tactics.
  • Sitting home can also locate Pokemon around, do you believe it?Sitting home can also locate Pokemon around, do you believe it?
    the tightening of the niantic developers' rules to prevent players from abusing the support tools also brings annoyance, such as those who have no conditions to move much, go away, it is hard to know. get the location of the pokemon around the area they live in
  • 5 undeniable benefits when playing Pokemon Go5 undeniable benefits when playing Pokemon Go
    get to know many new people, breathe fresh air, relieve stress, increase concentration thanks to going out for a walk .... are compelling reasons to force you to try pokemon go now .
  • Want to earn the fastest Pokécoins in Pokémon Go? So don't miss this article!Want to earn the fastest Pokécoins in Pokémon Go?  So don't miss this article!
    pokécoins in pokémon go play the role of buying items in the store. the more coins you earn, the more likely you are to buy more items. to earn pokécoins, players will have to complete certain tasks or buy real money.
  • Pokémon systems when fighting in Pokémon GoPokémon systems when fighting in Pokémon Go
    each pokémon system in pokémon go has different strengths, along with a specific weakness. this type of pokémon will have the power to attack the other pokémon, but can defeat the other pokémon. if you know the characteristics of each type, it will be easier to choose which pokémon to battle.
  • The terms you need to know when playing Pokémon GoThe terms you need to know when playing Pokémon Go
    pokémon go is the most prominent name in recent days. this game of capturing and training virtual animals has created a relatively new way of playing, as players have to constantly move to catch pokémon. during the process of joining pokémon go, you will encounter and use a lot of important terms. so what do they mean?
  • How to play Pokemon GO on Windows computersHow to play Pokemon GO on Windows computers
    recently, pokemon go has become a popular game, attracting thousands of gamers around the world. in previous posts network administrator has guided you on how to play pokemon go on android devices and ios devices. in the article below, network administrator will guide you how to play pokemon go on windows computers using bluestacks emulator software.
  • How to become a good Pokémon Go hunter?How to become a good Pokémon Go hunter?
    with a new way of playing, pokémon go has created a new phenomenon in the game village around the world. players will have to go around and find pokémon for themselves. so how to quickly catch pokémon and in large numbers?