M
M
MatrikLog2021-09-22 13:08:36
React
MatrikLog, 2021-09-22 13:08:36

How to immutably remove an object by key from a redux store?

Hello everyone, how can I delete an object immutably by key keyfrom stror redux via dispatch? I know the option through lodash, but I would like to understand how this can be done natively. deleteWill 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

1 answer(s)
A
Alexandroppolus, 2021-09-22
@MatrikLog

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 question

Ask a Question

731 491 924 answers to any question