I have the following code which is supposed to check if an object exists in state and replace it and if not append it to the state. So far appending to the state works correctly but when replacing the object the UI is not updated accordingly.
const [roomData, setRoomData] = useState([]);
.............
onSelectRoom={(room) => {
let roomIndex = roomData.findIndex(
(room) => room.roomId == item.id
);
roomData[roomIndex]
? (roomData[roomIndex] = room)
: setRoomData([...roomData, room]);
setRoomTotals([]);
}}