Let's say you have a store like this. And a component where you select a car from the list which gets passed as a prop to another component where you can edit various properties of the car object.
If the car objects had more properties like brand, weight etc. I would have to either:
Create a new mutation for every property This has the downside of causing a huge amount of mutations, which will make the store file very unreadable in my opinion.
Create a generic mutation that takes an object, a field, and a value and assign that. This has the benefit of only having one generic mutation. But it has the downside that it can't handle nested objects.
Create a new
carobject in the component and replace that in the state byidfield. This feels like a clean solution but it would involve copying the object to remove mutability, and that feels less clean. And also slow down the app if use a lot I guess.
In my projects, I've ended up with a mix of these options which just feels chaotic.
So, my question is if there's a better pattern to use than any of these? And if not, which is considered the best practice. I've searched for an answer to this for a long time but never found any good explanation.
state: {
cars: [
{id: 1, color: "blue"},
{id: 2, color: "blue"},
{id: 3, color: "blue"},
]
},
mutations: {
setColor(state, {car, color}){
car.color = color;
}
},
getters: {
allCars(state){
return state.cars;
}
},