mirror of
https://github.com/duckietm/Nitro-V3.git
synced 2026-06-19 15:06:20 +00:00
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:
@@ -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;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user