Skip to content

Instantly share code, notes, and snippets.

@kimberleehowley
Created September 9, 2020 22:19
Show Gist options
  • Save kimberleehowley/d5de25ef6c345489fb5138f05497cae1 to your computer and use it in GitHub Desktop.
Save kimberleehowley/d5de25ef6c345489fb5138f05497cae1 to your computer and use it in GitHub Desktop.
Sample code for wiring up event listeners in a Daily-powered React video chat app
// In Call.js
useEffect(() => {
if (!callObject) return;
const events = [
"participant-joined",
"participant-updated",
"participant-left"
];
function handleNewParticipantsState(event) {
event && logDailyEvent(event);
dispatch({
type: PARTICIPANTS_CHANGE,
participants: callObject.participants()
});
}
// Use initial state
handleNewParticipantsState();
// Listen for changes in state
for (const event of events) {
callObject.on(event, handleNewParticipantsState);
}
// Stop listening for changes in state
return function cleanup() {
for (const event of events) {
callObject && callObject.off(event, handleNewParticipantsState);
}
};
}, [callObject]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment