'Calling Action when state property changes
What is the best way for me to trigger an action anytime a specific property has been changed within the state? In this example the toggle property can be mutated from multiple places within the application. Any time toggle is mutated, i want to follow it up with executing the action toggleChanged. How would this be done? Ideally somehow within the state so the triggering is more easily manageable.
/**
* General app properties
*/
import { defineStore } from "pinia";
import { collapse } from "myutils";
export const useAppStore = defineStore("app", {
state: () => ({
toggle: false,
}),
actions: {
// anytime the toggle property has changed id like to trigger this event
async toggleChanged(props) {
console.log('Changed:', this.toggle)
collapse(this.toggle)
},
},
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
