Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix useRemoteParticipant re-rendering on participant events #891

Merged
merged 3 commits into from
Jun 11, 2024

Conversation

mpnri
Copy link
Contributor

@mpnri mpnri commented Jun 11, 2024

Hi,
This PR fixes the problem of not re-rendering in useRemoteParticipant for triggered participant events.

Currently, this hook uses useObservableState, which itself uses a useState to execute a re-render after each emit value in the observable. But we know that the reference of participant object does not change when new events are fired, and thus re-rendering does not occur. So, to solve this problem, we need to put the participant in a wrapper object to use useState.

P.S:
Maybe a cleaner coding to solve this problem:
(although it requires the 'map' operator from rxjs)

const observable = React.useMemo(
  () =>
    connectedParticipantObserver(room, identity, { additionalEvents: updateOnlyOn }).pipe(
      map((p) => ({ p })),
    ),
  [room, identity, updateOnlyOn],
);
const { p: participant } = useObservableState(observable, {
  p: room.getParticipantByIdentity(identity) as RemoteParticipant | undefined,
});

Copy link

changeset-bot bot commented Jun 11, 2024

🦋 Changeset detected

Latest commit: ae6bd2c

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

This PR includes changesets to release 5 packages
Name Type
@livekit/components-react Patch
@livekit/component-example-next Patch
@livekit/components-js-docs Patch
@livekit/component-docs-storybook Patch
@livekit/components-docs-gen 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

Copy link
Contributor

@lukasIO lukasIO left a comment

Choose a reason for hiding this comment

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

Thanks for flagging this.
It's very unfortunate that this is needed, but I cannot think of a better workaround, so I think your changes look good.
Only a couple of nits.

@@ -2,7 +2,7 @@ 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';
// import { useObservableState } from './internal';
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you delete this import?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

);
return participant;

const [participantWrapper, setParticipantWrapper] = React.useState({
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a comment outlining why this is needed? It'll make it easier to understand in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, is it understandable?

@lukasIO
Copy link
Contributor

lukasIO commented Jun 11, 2024

Looks good, thanks!

@lukasIO lukasIO merged commit 857f77e into livekit:main Jun 11, 2024
3 checks passed
@github-actions github-actions bot mentioned this pull request Jun 11, 2024
@mpnri mpnri deleted the patch-1 branch June 12, 2024 07:18
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.

2 participants