Код: Выделить всё
const onMessage = (event: MessageEvent) => {
try {
const message = JSON.parse(event.data);
const normalizedMsg = Array.isArray(message) ? message : [message];
bufferRef.current = [...bufferRef.current, ...normalizedMsg];
if (timeoutRef.current) clearTimeout(timeoutRef.current);
timeoutRef.current = setTimeout(() => {
const flushed = [...bufferRef.current];
setFixtures(prev => [...prev, ...flushed]);
bufferRef.current = [];
}, DEBOUNCE_MS);
} catch (error) {
console.error('Error parsing message', error);
}
};
Код: Выделить всё
const flushed = [...bufferRef.current];
setFixtures(prev => [...prev, ...flushed]);
bufferRef.current = [];
Код: Выделить всё
setFixtures(prev => [...prev, ...bufferRef.current]);
bufferRef.current = [];
- Обновления состояния React синхронны по расписанию, но асинхронны при применении
- Устаревшее замыкание в setTimeout
- объяснение, основанное на взаимодействии МЕЖДУ установленными таймаутами
Подробнее здесь: https://stackoverflow.com/questions/798 ... ading-befo
Мобильная версия