I have a state the is an array of strings, containing all the messages recived. And a useEffect that triggers when a new message is recived. So I try to use set state to append the new message to the message board but it generates a starnge result.
Original idea: spreading the contents to the state as usual.
useEffect(() => {
socket.on("recive_message", (data) => {
setBoard((chats) => [data.message, ...chats])
})
}, [])
but this way there is no appending and the chat board first element is being replaced with the incoming message.. So I tried another way I saw on the internet:
useEffect(() => {
socket.on("recive_message", (data) => {
setBoard([data.message, ...board])
})
}, [])
And this works just fine. What is the difference between the two syntax?