A plugin that allows you to monitor and react to changes in your store's state deeply.
Supports Pinia v2. (Vue 2 and 3)
npm i pinia-plugin-watch
yarn add pinia-plugin-watch # yarn
pnpm add pinia-plugin-watch # pnpm
import { createPinia } from 'pinia';
import { WatchPiniaPlugin } from 'pinia-plugin-watch';
const pinia = createPinia();
pinia.use(WatchPiniaPlugin);
import { defineStore } from 'pinia';
const useStore = defineStore('store', {
state: () => ({
count: 0,
user: {
name: 'John',
age: 20,
},
}),
// PiniaPluginWatch
watch: {
// Watch `count`
count: (newValue, oldValue, onCleanup, store) => {
console.log('count changed', newValue, oldValue);
},
// Watch `user` and `user.name`
user: {
handler: (newValue, oldValue, onCleanup, store) => {
console.log('user changed', newValue, oldValue);
},
children: {
name: (newValue, oldValue, onCleanup, store) => {
console.log('user.name changed', newValue, oldValue);
},
},
},
},
});
For usage examples, see the Usage documentation.
- Type:
Record<string, WatchHandler | WatchOptions>
- Type:
<T>(newValue: T, oldValue: T) => void
- Type:
Record<string, WatchHandler | WatchOptions>
- Properties:
handler
:WatchHandler
children?
:Record<string, WatchHandler | WatchOptions>
deep?
:boolean
(default:true
)
- Type:
typeof options.watch
The watch
option is copied to the $watch
property of the store.