I have a class instance in my react state and it's like below:
class Room {
name;
participant;
constructor(name) {
this.name = name;
this.participant = {
screensharing: false,
};
}
sendReinvite() {
console.log("reinvite");
}
}
after changing screen share and using the spread operator for mutating state, sendReinvite method is deleted and causes an error in the second click:
const SpreadOperator = () => {
const [room, changeRoom] = useState(new Room());
const toggleScreenSharing = () => {
console.log("room", room);
room.participant.screensharing = !room.participant.screensharing;
room.sendReinvite();
changeRoom({...room});
};
return (
<div>
<h1>spread operator</h1>
<button onClick={toggleScreenSharing}>Screen share</button>
</div>
);
};
export default SpreadOperator;
error: Uncaught TypeError: room.sendReinvite is not a function how can I change screen share without losing methods, by the way, that instance comes from some library and I can't change that,
check this like for a real one: