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

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

@ -58,7 +58,7 @@ const MessageGroup = forwardRef<
(key: string, message?: Message) => {
let length = 0;
setMessages(({ [key]: unused, ...rest }) => {
const { [key]: unused, ...rest } = messages;
const result: Messages = rest;
if (message) {
@ -69,10 +69,9 @@ const MessageGroup = forwardRef<
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;
setMessages((previous) => {
const result: Messages = {};
Object.keys(previous).forEach((key: string) => {
Object.keys(messages).forEach((key: string) => {
if (re.test(key)) {
assignMessage?.call(null, result, key);
} else {
result[key] = previous[key];
result[key] = messages[key];
length += 1;
}
});
onSet?.call(null, length);
return result;
});
setMessages(result);
},
[onSet],
[messages, onSet],
);
const messageElements = useMemo(() => {

Loading…
Cancel
Save