Fix chat react state getting overwritten. Closes #1529

This commit is contained in:
Gabe Kangas 2021-12-12 00:11:38 -08:00
parent f3a8266c99
commit 70027d058f
No known key found for this signature in database
GPG Key ID: 9A56337728BC81EA

View File

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