Use findIndex to find the activeTable instead, and assign a new array to the .list which contains a new activeTable object:
const newState = { ...state };
const { list } = newState;
const activeTableIndex = list.findIndex((table) => table.id === action.id);
const newActiveTable = { ...list[activeTableIndex] };
if (newActiveTable.visible === undefined) newActiveTable.visible = false;
else delete newActiveTable.visible;
newState.list = [...list.slice(0, activeTableIndex), newActiveTable, list.slice(activeTableIndex + 1)];
return newState;
Or, if there will never be more than one matching id, you might consider .map to be more elegant:
const newState = { ...state };
const newList = newState.list.map((table) => {
if (table.id !== action.id) return table;
const newActiveTable = { ...table };
if (newActiveTable.visible === undefined) newActiveTable.visible = false;
else delete newActiveTable.visible;
return newActiveTable;
});
newState.list = newList;
return newState;