score:2

Accepted answer
case "toggle option":
  const isCurrentlySelected = state.selectedOptions.includes(
    action.payload.name
  );

  let newSelectedOptions = state.selectedOptions;

  if (isCurrentlySelected) {
    newSelectedOptions = newSelectedOptions.filter(
      (item) => item !== action.payload.name
    );
  } else {
    newSelectedOptions.concat(action.payload.name); // <-- add to and return new array
  }

  return {
    ...state,
    selectedOptions: newSelectedOptions,
  };

Related Query

More Query from same tag