Upon analyzing redux and ngrx, it appears that immer is the preferred library for creating a copy of the state before storing it. In following the example provided by immer, I implemented the following code in my reducer:
on(exampleActions.updateExample, (state, { example }) => {
return produce((state: ExampleType, draft: ExampleType) => {
draft.push({ example });
draft[1].done = true;
});
})
However, TypeScript raises a no-shadowed-variable
error, which clashes with the example given. Furthermore, I encounter issues with return-type errors when trying to return a value.
In scenarios where example
is a multi-level object:
const example = {
a: {
b: { c: 1 }
}
};
I realize that draft
must be completely dereferenced as well.
There aren't many immersive examples available for integrating createReducer
as this was a recent update for 2019. Should I disable the no-shadowed-variable
rule for immer or is there a more effective approach to ensure that both state
and example
are correctly dereferenced, especially since example
consists of nested objects?
As an alternative, I could opt out of using immer and utilize ramda's clone
function or endeavor to manually deep-copy everything.