Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Pinia is a state management library for Vue.js applications. It serves as a store for shared state, with a simple and straightforward API. It is often used as an alternative to Vuex and provides reactive data stores that can be accessed throughout a Vue application.
Defining a Store
This feature allows you to define a new store with a unique identifier, state, and actions. The state is reactive and can be accessed and manipulated across components.
import { defineStore } from 'pinia';
export const useCounterStore = defineStore('counter', {
state: () => ({
count: 0
}),
actions: {
increment() {
this.count++;
}
}
});
Accessing Store State in Components
This code demonstrates how to access and use the store state within a Vue component. The store's reactive state is used in the template, and actions can be called to update the state.
<template>
<div>{{ counterStore.count }}</div>
<button @click="counterStore.increment">Increment</button>
</template>
<script setup>
import { useCounterStore } from './stores/counter';
const counterStore = useCounterStore();
</script>
Persisting State
Pinia allows for easy integration with other libraries, such as VueUse, to persist state between page reloads using local storage or other methods.
import { defineStore } from 'pinia';
import { useLocalStorage } from '@vueuse/core';
export const useUserStore = defineStore('user', {
state: () => ({
name: useLocalStorage('user-name', '')
})
});
Vuex is the official state management library for Vue.js. It is more complex and verbose than Pinia, with a strict set of rules and patterns such as mutations for state changes. Pinia offers a simpler and more flexible API compared to Vuex.
Redux is a state management library for JavaScript apps, not limited to Vue. It uses a single immutable state tree and pure reducer functions for state updates. Redux is known for its strict unidirectional data flow, which can be more complex than Pinia's more Vue-centric and straightforward approach.
MobX is a state management library that focuses on reactive programming. It uses observables to make state management simple and scalable. Unlike Pinia, which is designed specifically for Vue, MobX can be used with any framework, and it emphasizes transparent functional reactive programming (TFRP).
Intuitive, type safe and flexible Store for Vue
To learn more about Pinia, check its documentation.
FAQs
Intuitive, type safe and flexible Store for Vue
We found that pinia demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.