Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event listener with redux global state #86

Open
drheinheimer opened this issue Dec 22, 2021 · 1 comment
Open

Event listener with redux global state #86

drheinheimer opened this issue Dec 22, 2021 · 1 comment

Comments

@drheinheimer
Copy link

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

}
@elvis-pn
Copy link
Contributor

Do you have code I could use to reproduce this issue? From just that snippet its hard to know exactly what's causing the trouble.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants