Garmaine Staff asked 2 years ago

I have a generator function in a Redux Saga that gets an ID, gets the state and dispatches a an action with the state without the object key with the matching id in it. The object in the reducer it should've replaced not mutated looks like this:

{
    [CURRENT YEAR]: {
      [DAY OF YEAR]: {
        [ID THAT SHOULD BE DELETED]: {}
      },
    },
};

When this function ran using the commented out delete operator it would mutate the entire state in redux and in ALL Redux loggers (prev and after change) and my selectors would return the previous memoized version because no change was detected. I solved this by creating a new object with lodash Omit, but is there any faster or < 21kb solution to this?

function* removeExerciseAsync(action: any) {
  const { currentDate, currentYear }: DateState = yield select((state: AppState) => state.date);
  const years = yield select(selectYears);

  const dayOfYear = currentDate.dayOfYear();


  const newYearState: DaySummaryTypes = Object.assign({}, years[currentYear]);

  const exerciseId = action.payload;

  if (!newYearState[dayOfYear][exerciseId]) return;

  // delete newYearState[dayOfYear][exerciseId];

  const newState = omit(newYearState, `${dayOfYear}.${exerciseId}`);

  yield put(updateYearExercises({ [currentYear]: newState }));
}