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

Loading…
Cancel
Save