I write a lot of boilerplate code like this
const state = context.getState();
context.patchState({
someForm: {
...state.someForm,
model: {
...state.someForm.model,
someFlag: false
}
}
});
What I would like to have is something like this:
context.patchStatePath(['someForm', 'model', 'someFlag'], false)
There should be some helper for deep updates. For now, I write a lot of redux-style immutability stuff
I am working on a built-in immutability helper that will help in this regard. I'll keep you posted.
I think immer is a great solution for this kinda of stuff. Ideally your state isn't too deep so this isn't a problem but I agree.
Here's an Immer with NGXS article, hope this helps! https://blog.angularindepth.com/simple-state-mutations-in-ngxs-with-immer-48b908874a5e
Immer is exactly what I need.
@AlgoTrader
Currently we've got a lot of features that allow to reduce boilerplate code with ... object descruction:
@markwhitfeld
deepMerge<T, S>(target: T, source: S): T & S {
for (let [k, v] of Object.entries(source)) {
if (!(k in target))
target[k] = source[k];
else if (v.constructor.name === 'Object')
this.deepMerge(target[k], source[k]);
else
target[k] = source[k];
}
return target as T & S;
}
Here's an Immer with NGXS article, hope this helps! https://blog.angularindepth.com/simple-state-mutations-in-ngxs-with-immer-48b908874a5e
broken link now :(
Here's an Immer with NGXS article, hope this helps! https://blog.angularindepth.com/simple-state-mutations-in-ngxs-with-immer-48b908874a5e
broken link now :(
That is the one https://medium.com/angular-in-depth/simple-state-mutations-in-ngxs-with-immer-48b908874a5e
Most helpful comment
I am working on a built-in immutability helper that will help in this regard. I'll keep you posted.