Skip to content

Event listener with redux global state #86

Description

@drheinheim

I'm trying to update a global redux state with pubnub events, but cannot, as the event listener does not recognize a redux-based global state variable even though, oddly, it can update the state. Here's my pseudo-code:

const MyComponent = () => {
  const {messages} = useSelector(state => state.chatstore)
  const dispatch = useDispatch();
  useEffect(() => {
    pubnub.subscribe(channels)
    pubnub.addEventListener(pubnubListener)
  }, [pubnub, channels])

  const pubnubListener = event => {
    const {message} = event
    console.log(messages) // returns nothing! I can't do any logic here related to all accumulated messages
    dispatch({type: ADD_MESSAGE, message: newMessage}) // This works, and...
      // ...I can do something with all messages within the reducer after dispatching
  }

  return (
    <div>
    {messages.map(message => <p key={message.id}>{message}</p>)}
    </div>
  )

}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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