fix(room): word-quiz countdown mutates Map value objects in place

The 1s tick did `value.secondsLeft--` on the VoteValue objects held by the
previous state Map and returned the same Map reference when nothing expired — an
in-place state mutation (breaks memoization / StrictMode double-invoke). Rebuild
new value objects in a new Map each tick (and drop expired entries).
This commit is contained in:
simoleo89
2026-06-13 16:20:59 +02:00
parent a2f8a4dd61
commit 648cea698d
+10 -8
View File
@@ -112,19 +112,21 @@ const useWordQuizWidgetState = () =>
{ {
setUserAnswers(prevValue => setUserAnswers(prevValue =>
{ {
const keysToRemove: number[] = []; if(prevValue.size === 0) return prevValue;
prevValue.forEach((value, key) => // Build new value objects + a new Map — don't decrement
// secondsLeft on the objects still referenced by prevValue
// (in-place mutation breaks memoization / StrictMode replay).
const next = new Map(prevValue);
next.forEach((value, key) =>
{ {
value.secondsLeft--; const secondsLeft = value.secondsLeft - 1;
if(value.secondsLeft <= 0) keysToRemove.push(key); if(secondsLeft <= 0) next.delete(key);
else next.set(key, { ...value, secondsLeft });
}); });
if(keysToRemove.length === 0) return prevValue;
const next = new Map(prevValue);
keysToRemove.forEach(key => next.delete(key));
return next; return next;
}); });
}; };