Skip to content

useState troubles to update when receiving new events #125

Open
@SergeyMelnyk

Description

@SergeyMelnyk

Hello everyone, please help with an issue I encountered while using pusher-websocket-react-native. I'm trying to implement a small chat, and the problem is that when I receive a notification in the onEvent method and try to update my local state, the messages value always equals an empty array.

cosnt [messages, setMessages] = useState([])
useEffect(() => {
      getInitialMessagesByRestApi().then(res => {setMessages(res.data)})
}, [])

...
const onEvent = () => {
const onEvent = event => {
    const parsedData = JSON.parse(event.data)

   setMessages([...messages, event.data]) <-- messages in this place always empty
}

I already tried to get initial messages after pusher init, it works but when i am gettig few events like one by one it , messages array always equal to initial without prev message from pusher events

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions