fix(striker-ui): don't nest (potential) setState() in MessageGroup

main
Tsu-ba-me 2 years ago
parent 9d02da717c
commit 6832933d7f
  1. 48
      striker-ui/components/MessageGroup.tsx

@ -58,21 +58,20 @@ const MessageGroup = forwardRef<
(key: string, message?: Message) => {
let length = 0;
setMessages(({ [key]: unused, ...rest }) => {
const result: Messages = rest;
const { [key]: unused, ...rest } = messages;
const result: Messages = rest;
if (message) {
result[key] = message;
}
if (message) {
result[key] = message;
}
length = Object.keys(result).length;
length = Object.keys(result).length;
onSet?.call(null, length);
onSet?.call(null, length);
return result;
});
setMessages(result);
},
[onSet],
[messages, onSet],
);
const setMessageRe = useCallback(
(re: RegExp, message?: Message) => {
@ -84,25 +83,22 @@ const MessageGroup = forwardRef<
length += 1;
}
: undefined;
const result: Messages = {};
Object.keys(messages).forEach((key: string) => {
if (re.test(key)) {
assignMessage?.call(null, result, key);
} else {
result[key] = messages[key];
length += 1;
}
});
setMessages((previous) => {
const result: Messages = {};
Object.keys(previous).forEach((key: string) => {
if (re.test(key)) {
assignMessage?.call(null, result, key);
} else {
result[key] = previous[key];
length += 1;
}
});
onSet?.call(null, length);
onSet?.call(null, length);
return result;
});
setMessages(result);
},
[onSet],
[messages, onSet],
);
const messageElements = useMemo(() => {

Loading…
Cancel
Save