Created
September 9, 2020 22:19
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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