This repository has been archived by the owner on Aug 24, 2023. It is now read-only.
Fix disconnect on onmount regression due to strict mode #112
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #111
React 18's strict mode calls all
useEffect
hooks twice.This generally leads to the the useEffect cleanup function being called on a component, even though it never unmounted.
Within
useRoom
in this package, theroom
state was initialized withuseState(new Room())
.Because we want the LiveKitRoom component to automatically disconnect when unmounted, this lead to a disconnect call being called on the only existing instance of
room
, when strict-mode's second hook execution invoked the cleanup function. Because this was happening while the room was already connecting it made it effectively impossible to connect to a room in dev mode.A previous attempt to fix this caused the auto-disconnect logic not to fire at all on component unmount.
This fix re-establishes auto-disconnect behaviour and moves
room
instantiation into auseEffect
hook in order to preventdisconnect
being called on the only present room instance.This changes the assumption that
useRoom()
always directly returns a validRoom
object. With this change it can also happen thatroom
isundefined
on the first render, before the useEffect has set its value.