Skip to content

fix: keep useRoom from disconnecting when sortParticipants changes identity - #471

Closed
RaphaelFakhri wants to merge 1 commit into
livekit:mainfrom
RaphaelFakhri:w14-useroom-inline-sort
Closed

RaphaelFakhri wants to merge 1 commit into
livekit:mainfrom
RaphaelFakhri:w14-useroom-inline-sort

Conversation

@RaphaelFakhri

Copy link
Copy Markdown

Fixes useRoom disconnecting the room and re-rendering in a loop when sortParticipants is an inline function.

Problem

useRoom used the sort function as a dependency of its effect, and the effect cleanup calls room.disconnect(). A function passed inline, such as useRoom(room, { sortParticipants: (p) => p.reverse() }), has a new identity on every render. Each render therefore ran the cleanup, disconnected the room, and re-ran the effect. The effect also sets a new audioTracks array, which triggers another render, so the hook renders in a loop.

Change

Store the current sort function in a ref that useRoom updates on every render, and depend only on room in the effect. The hook always uses the latest sort function and disconnects only when the room changes or the component unmounts.

Tests

src/__tests__/useRoom.test.tsx covers three cases:

  • Re-rendering with a new inline sortParticipants does not disconnect the room.
  • Unmounting disconnects the room once.
  • After a re-render, participant updates use the latest sort function.

Without the change, the first test fails because the hook re-renders in a loop. With it, all three pass. tsc and eslint are clean.

The commit also adds react-test-renderer as a dev dependency and excludes __tests__ from tsconfig.build.json, so that hook tests can run and stay out of the build.

Changeset included.

Fixes #470

@CLAassistant

CLAassistant commented Sep 29, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@changeset-bot

changeset-bot Bot commented Sep 29, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5b008cb

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

This PR includes changesets to release 1 package
Name Type
@livekit/react-native Patch

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

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Devin Review found 1 potential issue.

1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

Comment thread src/useRoom.ts
room.disconnect();
};
}, [room, sortFunc]);
}, [room]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Participant order stays stale after sort changes

When sortParticipants changes without a room event, useRoom leaves the existing participant order untouched. sortFuncRef affects only future event callbacks, so the new ordering never appears until another room event.

Learn more

The hook stores the sorted participant list in React state. Changing the sorting callback previously reran the effect, which called onSubscribedTrackChanged() and rebuilt that list. The new effect dependency excludes the callback, so changing it alone does not update the visible order. A room with no subsequent events retains its previous ordering indefinitely.

Example: A call view initially sorts Alice before Bob. The user selects a reverse-order control that replaces sortParticipants, but the view still shows Alice first until someone joins, leaves, or generates another subscribed event.

Recommended fix: Separate participant-list recalculation from room subscription and disconnect cleanup. Recalculate the list when the sorting callback changes without tearing down the room, while preserving the stable event subscription for inline callbacks.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

useRoom disconnects the room on every render with an inline sortParticipants

2 participants