In a redux reducer, here is an initial state:
const initialState = {
logs: {
error: false,
errorMessage: '',
pending: false,
fulfilled: false,
lines: '',
},
};
I would like to first reset to this state and then update lines.
One way I can do that -
return { logs: { ...initialState.logs, lines: action.payload.data } }
Another way (which I'm not 100% sure is right) -
draft.logs = { ...initialState.logs };
draft.logs.lines = action.payload.data;
What would be the best way to do that? Can it be done without using spread?
Thanks.
(using latest immer version)
Thanks in advance.
I'd try Object.assign(draft, initialState); draft.logs.lines = action.payload.data.
Note that this doesn't strictly reset draft, as it won't remove keys that are present on state but didn't exist in the initialstate. If that is necessary, you could create a fresh draft from the initial state and return that (although it is probably simpler to not use immer in that very specific case):
const newStateDraft = createDraft(initialState)
newStateDraft.logs.lines = action.payload.data)
return finishDraft(newStateDraft)
```
Closing as answered. Will reopen in case the answer doesn't suffice :)
Thanks @mweststrate !
Most helpful comment
I'd try
Object.assign(draft, initialState); draft.logs.lines = action.payload.data.Note that this doesn't strictly reset draft, as it won't remove keys that are present on
statebut didn't exist in the initialstate. If that is necessary, you could create a fresh draft from the initial state and return that (although it is probably simpler to not use immer in that very specific case):```