i have a function that gets called by a socket.io's event:
const [room, setRoom] = useState({})
const [messages, setMessages] = useState([])
function reciveMessage(message) {
if (room.id == message.roomid) {
setMessages(messages => [...messages, message])
}
}
the room state value is stale
if i try to work around it by
setRoom(room => {
if (room.id == event.data.source.conId) {
setMessages(messages => [...messages, message])
}
return room
})
the room value is the lastest but setMessage runs twice is there a better way?