pusher / pusher/pusher-websocket-react-native
useState troubles to update when receiving new events
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 76
- Forks
- 64
- PR merge metrics
- No merged PRs in 30d
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the onEvent callback and the useState/useEffect example in the issue, then determine whether the behavior belongs to the SDK or the consuming React Native code. A useful outcome would be a minimal reproduction or a confirmed explanation of which component is responsible.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- react-native, typescript
- Domain
- mobile
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100