The application's state is primarily managed by the Twilio Video SDK room object rather than external state management tools like Redux. The room object acts as the single source of truth and is an EventEmitter.
To interact with the room in development, the application exposes it globally as window.twilioRoom in the browser console.
To react to changes in the room (like a dominant speaker changing), use React hooks to subscribe to and unsubscribe from room events. This ensures components re-render only when relevant state changes occur.
import { useEffect, useState } from 'react';
export default function useDominantSpeaker(room) {
const [dominantSpeaker, setDominantSpeaker] = useState(room.dominantSpeaker);
useEffect(() => {
room.on('dominantSpeakerChanged', setDominantSpeaker);
return () => {
room.off('dominantSpeakerChanged', setDominantSpeaker);
};
}, [room]);
return dominantSpeaker;
}