Skip to content

Commit

Permalink
Fix useRemoteParticipant re-rendering on participant events (#891)
Browse files Browse the repository at this point in the history
Co-authored-by: lukasIO <[email protected]>
  • Loading branch information
mpnri and lukasIO authored Jun 11, 2024
1 parent d4d8add commit 857f77e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-rocks-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@livekit/components-react": patch
---

Fix `useRemoteParticipant` re-rendering on participant events
18 changes: 12 additions & 6 deletions packages/react/src/hooks/useRemoteParticipant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { connectedParticipantObserver } from '@livekit/components-core';
import type { ParticipantEvent, RemoteParticipant } from 'livekit-client';
import * as React from 'react';
import { useRoomContext } from '../context';
import { useObservableState } from './internal';

/** @public */
export interface UseRemoteParticipantOptions {
Expand Down Expand Up @@ -35,9 +34,16 @@ export function useRemoteParticipant(
() => connectedParticipantObserver(room, identity, { additionalEvents: updateOnlyOn }),
[room, identity, updateOnlyOn],
);
const participant = useObservableState(
observable,
room.getParticipantByIdentity(identity) as RemoteParticipant | undefined,
);
return participant;

// Using `wrapperParticipant` to ensure a new object reference,
// triggering a re-render when the participant events fire.
const [participantWrapper, setParticipantWrapper] = React.useState({
p: room.getParticipantByIdentity(identity) as RemoteParticipant | undefined,
});
React.useEffect(() => {
const listener = observable.subscribe((p) => setParticipantWrapper({ p }));
return () => listener.unsubscribe();
}, [observable]);

return participantWrapper.p;
}

0 comments on commit 857f77e

Please sign in to comment.