Код: Выделить всё
function BattleEventListener({
roomId
}: BattleEventListenerProps) {
const dispatch = useDispatch();
const roomState = useSelector((state: RootState) => state.rooms.byId[roomId]);
const roomRef = useRef(roomState);
useEffect(() => {
if (gameHub.hub === null) return;
console.info("Connecting room to gameHub.");
gameHub.hub.on('BattleEvents', handleBattleEvents);
return () => {
gameHub.hub?.off('BattleEvents', handleBattleEvents);
}
}, [dispatch, gameHub.hub, roomRef.current]);
useEffect(() => {
roomRef.current = roomState;
}, [roomState]);
return null;
async function handleBattleEvents (evtId: string, roomId: string, events: BattleEvent[]) {
console.debug('
Подробнее здесь: [url]https://stackoverflow.com/questions/79694840/problem-with-accessing-stale-redux-state-in-event-handlers[/url]