diff options
Diffstat (limited to 'vnext/src/components/Chat.js')
-rw-r--r-- | vnext/src/components/Chat.js | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/vnext/src/components/Chat.js b/vnext/src/components/Chat.js index 9ac2f2e5..0742b7fb 100644 --- a/vnext/src/components/Chat.js +++ b/vnext/src/components/Chat.js @@ -14,18 +14,18 @@ import './Chat.css'; export default function Chat(props) { const [chats, setChats] = useState([]); - useEffect(() => { - console.log(props.connection); - if (props.connection) { + useEffect(() => { + if (props.connection.addEventListener) { props.connection.addEventListener('msg', onMessage); } loadChat(props.match.params.user); + console.log(props.connection); return () => { - if (props.connection) { + if (props.connection.removeEventListener) { props.connection.removeEventListener('msg', onMessage); } }; - }, [props.connection]); + }, [props.connection.readyState]); let loadChat = (uname) => { const { hash } = props.visitor; @@ -41,7 +41,9 @@ export default function Chat(props) { let onMessage = (json) => { const msg = JSON.parse(json.data); if (msg.user.uname === props.match.params.user) { - setChats([msg, ...chats]); + setChats((oldChat) => { + return [msg, ...oldChat]; + }); } }; @@ -79,5 +81,5 @@ export default function Chat(props) { Chat.propTypes = { visitor: UserType.isRequired, match: ReactRouterPropTypes.match.isRequired, - connection: PropTypes.shape.isRequired + connection: PropTypes.object.isRequired }; |