When working in the store, I have an action that updates certain data. The action is structured like this:
setRoomImage({ state }, { room, index, subIndex, image }) {
state.fullReport.rooms[room].items[index].items[subIndex].image = image;
console.log(state.fullReport.rooms[room].items[index].items[subIndex])
},
Since the data is dynamic, I cannot hard code the properties and need to dynamically change nested values. Here's how the data appears:
fullreport: {
rooms: {
abc: {
items: [
{
type: "image-only",
items: [
{
label: "Main Image 1",
image: ""
},
{
label: "Main Image 2",
image: ""
}
]
}
]
}
}
}
After dispatching the action, although I see successful mutation of the sub-property image
in the console, the value doesn't change when accessing the VueX store from the Vue DevTools within Chrome. This is what the console displays:
https://i.stack.imgur.com/z2Smr.png
I'm wondering why this is happening. Even though the data is visibly changing, the state isn't reflecting it, leading to no rerendering of components.
I attempted using Vue.set
instead of a simple assignment, but had no success :(
Vue.set(
state.fullReport.rooms[room].items[index].items[subIndex],
"image",
image
);
Edit:
In response to suggestions by David Gard, I tried the following:
Additionally, I rely on Lodash _
(I understand duplicating entire objects isn't ideal), here is the updated mutation code block.
let fullReportCopy = _.cloneDeep(state.fullReport);
fullReportCopy.rooms[room].items[index].items[subIndex].image = image;
Vue.set(state, "fullReport", fullReportCopy);
Within the computed property where state.fullReport
serves as a dependency, I added a console.log
which outputs a string each time the computed property is recomputed.
Upon committing this mutation, I observe the computed property logging the string every time, but the received state remains unchanged. It seems that Vue.set
merely indicates to the computed property that the state has been altered without actually implementing the change. Consequently, there are no visible alterations in my component's user interface.