Skip to content
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

Merged
merged 2 commits into from
Dec 21, 2022

Conversation

lukasIO
Copy link
Contributor

@lukasIO lukasIO commented Dec 20, 2022

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, the room state was initialized with useState(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 a useEffect hook in order to prevent disconnect being called on the only present room instance.

This changes the assumption that useRoom() always directly returns a valid Room object. With this change it can also happen that room is undefined on the first render, before the useEffect has set its value.

@changeset-bot
Copy link

changeset-bot bot commented Dec 20, 2022

🦋 Changeset detected

Latest commit: c236376

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@livekit/react-components Minor
@livekit/react-core Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@lukasIO lukasIO requested a review from davidzhao December 20, 2022 22:06
Copy link
Member

@davidzhao davidzhao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good fix!

@lukasIO lukasIO merged commit 0273d9e into master Dec 21, 2022
@lukasIO lukasIO deleted the lukas/fix-strict-mode-unmount branch December 21, 2022 07:36
@github-actions github-actions bot mentioned this pull request Dec 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Participant not disconnected from the Room when pressing the browser back button
2 participants