I'm using immutability-helper to update state, in React. I used to update things like this:
const newElement = update(element, {
x: {
$apply: function(x) {
return x * width;
}
},
markers: {
$set: element.markers
}
});
But what about updating a property, which is an array. Let's say markers is an array, if I create the newElement by using $set
, it justs create a reference copy, hence, this:
newElement.markers[0].text = "sample"
will modify state. What would be the proper way to do that, other than this:
const newMarkers = [];
newElement.markers.forEach(marker => {
const updatedMarkers = update(marker, {
x: {
$apply: function(x) {
return x * width;
}
},
y: {
$apply: function(x) {
return x * height;
}
}
});
newMarkers.push(updatedMarkers);
});
newElement["markers"] = newMarkers;