I have normalized my data in a Vuex app but I haven't been able to find an answer on how to watch the data for changes. For example, here's a simplified version of my setup.
State:
const state = {
shapes: {
collection: [1, 2, 3],
data: {
1: {
type: 'circle',
isActive: false,
},
},
}
}
Getters:
const getters = {
findShape: (state) => (id) => {
return state.shapes.data[id];
}
allShapes: (state, getters) => {
return state.shapes.collection.map(id => getters.findShape(id));
}
}
In my component I'm calling the allShapes getter in a computed property and passing the shapes down to child components via a v-for.
computed: {
shapes() {
return this.$store.getters.allShapes;
}
}
Adding and removing shapes works fine. The problem is whenever I update a property in the data object the change isn't reflected in my components. Here is a mutation where I'm setting an isActive property:
const mutations = {
setActive(state, shape) {
Vue.set(state.shapes.data[shape.id], 'isActive', shape.isActive);
}
}
Any component that then references the isActive property doesn't get updated. I get why this is happening but I haven't found a solution other than going back to a nested array structure. Is there a way to trigger an update in the allShapes getter when the data object changes? Or is there a different way to approach pulling the data into my components to make it reactive?