Open maxime1992 opened 7 years ago
Your first suggested refactoring looks mutable to me:
case 'RATE': {
const talks = {...state.talks};
talks[action.payload.talkId] = {
...state.talks[action.payload.talkId],
rating: action.payload.rating
};
return {...state, talks};
}
You just mutate the talks object one level up in the nesting. Am I missing something?
The last refactoring you have looks immutable to me. So the change being more than mere syntax preference:
case 'RATE': {
return {
...state,
talks: {
...state.talks,
[action.payload.talkId]: {
...state.talks[action.payload.talkId],
rating: action.payload.rating
}
}
}
}
Is this a question for me @apasternack ? Because it's all what's this issue is about.
The first "suggested refactoring" is the code from the talk and the repo.
The second one is my proposal (which is exactly the same as your last part in comment) so I do not understand.
Hello @maxime1992, yeah it was a question. I was confused. I thought that first "suggested refactoring" was from you. Now it makes sense! Thanks, wanted to make sure I understood the bit about avoiding mutating state.
Hi Victor, after watching your talk about tackling state in Angular apps I wanted to dig into the code of this repo and while I was reading your reducer I noticed an immutability issue. Checkout model.ts#L49.
And it should be
Or, I have a preference for this syntax
I made a quick repro of the bug that you can run in chrome console
BTW your talk was great and I can't wait to give a try to ngrx v4 :).
Cheers