Skip to content

Event listener with redux global state #86

@drheinheim

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>
  )

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions