Answer the question
In order to leave comments, you need to log in
How to immutably remove an object by key from a redux store?
Hello everyone, how can I delete an object immutably by key key
from stror redux via dispatch? I know the option through lodash, but I would like to understand how this can be done natively. delete
Will the store mutate when deleted via delete
?
Here is an example of a store
const initialState = {
key : {
objKey1:val1,
objKey2:val2
}
};
Answer the question
In order to leave comments, you need to log in
Yes, delete will mutate the store.
the easiest way is to make a shallow copy of the object and delete it from it:
if (state.key[objKey]) {
const copy = {...state.key};
delete copy[objKey];
return { ...state, key: copy };
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question