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

Issue from stream #1

Closed
vancegillies opened this issue Aug 31, 2022 · 0 comments
Closed

Issue from stream #1

vancegillies opened this issue Aug 31, 2022 · 0 comments

Comments

@vancegillies
Copy link

don't know if you fixed this but I played around with the socket hook

i think your issue was this

return () => {
      socket.off('connected');
      // socket.off('onMessage'); < Need to comment out these in other useEffect
      // socket.off('onConversation');
      // socket.off('onMessageDelete');
    };
type SocketPayloadMap = {
  onMessage: MessageEventPayload,
  onConversation: Conversation,
  onMessageDelete: DeleteMessageResponse
}

//HOOK
const useSocket = <K extends keyof SocketPayloadMap>(event: K, listener: (payload: SocketPayloadMap[K]) => void) => {
  const socket = useContext(SocketContext);

  useEffect(() => {
    socket.on(event as string, listener)
    return () => {
      socket.off(event as string, listener)
    }
  }, [socket, event, listener])
}
// Usage
useSocket('onMessage', (payload) => {
  const { conversation, message } = payload;
  console.log(conversation, message);
  dispatch(addMessage(payload));
  dispatch(updateConversation(conversation));
})
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

1 participant