From 70027d058fb7a41ca03d08141a7ca1715a3a5721 Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Sun, 12 Dec 2021 00:11:38 -0800 Subject: [PATCH] Fix chat react state getting overwritten. Closes #1529 --- webroot/js/components/chat/chat.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/webroot/js/components/chat/chat.js b/webroot/js/components/chat/chat.js index 94c1fa873..5c7df8ce3 100644 --- a/webroot/js/components/chat/chat.js +++ b/webroot/js/components/chat/chat.js @@ -169,10 +169,14 @@ export default class Chat extends Component { const chatUserNames = allChatUserNames.filter( (name) => name != username ); - this.setState({ - messages: data.concat(this.state.messages), - chatUserNames, + this.setState((previousState, currentProps) => { + return { + ...previousState, + messages: data.concat(previousState.messages), + chatUserNames, + }; }); + this.scrollToBottom(); }) .catch((error) => { @@ -208,8 +212,8 @@ export default class Chat extends Component { if (messageType === SOCKET_MESSAGE_TYPES.CONNECTED_USER_INFO) { const modStatusUpdate = checkIsModerator(message); if (modStatusUpdate !== this.state.isModerator) { - this.setState({ - isModerator: modStatusUpdate, + this.setState((previousState, currentProps) => { + return { ...previousState, isModerator: modStatusUpdate }; }); } } @@ -252,8 +256,8 @@ export default class Chat extends Component { Math.max(updatedMessageList.length - 300, 0) ); } - this.setState({ - messages: updatedMessageList, + this.setState((previousState, currentProps) => { + return { ...previousState, messages: updatedMessageList }; }); } else if ( renderableChatStyleMessages.includes(messageType) && @@ -270,7 +274,10 @@ export default class Chat extends Component { ); newState.chatUserNames = [...updatedChatUserNames]; } - this.setState(newState); + + this.setState((previousState, currentProps) => { + return { ...previousState, newState }; + }); } // if window is blurred and we get a new message, add 1 to title