diff --git a/lib/socket/message-handler.react.js b/lib/socket/message-handler.react.js index 60858ada8..15a5ea318 100644 --- a/lib/socket/message-handler.react.js +++ b/lib/socket/message-handler.react.js @@ -1,51 +1,41 @@ // @flow -import PropTypes from 'prop-types'; import * as React from 'react'; +import { useDispatch } from 'react-redux'; import { processMessagesActionType } from '../actions/message-actions'; import { type ServerSocketMessage, serverSocketMessageTypes, type SocketListener, } from '../types/socket-types'; -import type { DispatchActionPayload } from '../utils/action-utils'; -import { connect } from '../utils/redux-utils'; type Props = {| - addListener: (listener: SocketListener) => void, - removeListener: (listener: SocketListener) => void, - // Redux dispatch functions - dispatchActionPayload: DispatchActionPayload, + +addListener: (listener: SocketListener) => void, + +removeListener: (listener: SocketListener) => void, |}; -class MessageHandler extends React.PureComponent { - static propTypes = { - addListener: PropTypes.func.isRequired, - removeListener: PropTypes.func.isRequired, - dispatchActionPayload: PropTypes.func.isRequired, - }; - - componentDidMount() { - this.props.addListener(this.onMessage); - } - - componentWillUnmount() { - this.props.removeListener(this.onMessage); - } - - render() { - return null; - } - - onMessage = (message: ServerSocketMessage) => { - if (message.type !== serverSocketMessageTypes.MESSAGES) { - return; - } - this.props.dispatchActionPayload( - processMessagesActionType, - message.payload, - ); - }; +export default function MessageHandler(props: Props) { + const { addListener, removeListener } = props; + + const dispatch = useDispatch(); + const onMessage = React.useCallback( + (message: ServerSocketMessage) => { + if (message.type !== serverSocketMessageTypes.MESSAGES) { + return; + } + dispatch({ + type: processMessagesActionType, + payload: message.payload, + }); + }, + [dispatch], + ); + React.useEffect(() => { + addListener(onMessage); + return () => { + removeListener(onMessage); + }; + }, [addListener, removeListener, onMessage]); + + return null; } - -export default connect(null, null, true)(MessageHandler);