Skip to content

How can I reflect membership state updates? #3029

Open
@Liamandrew

Description

@Liamandrew

Hi,

I have a scenario where users can view channels they aren't a member of.

When viewing a channel they aren't a member of, I show a "Subscribe" button which should add themselves to the channel. Conversely, if they are a member of said channel then there is an "Unsubscribe" button which removes themselves from the channel.

To drive this logic to determine which button to show, I'm using channel.state.membership?.status. However, because I'm also using context to store the channel/thread (similar to this) between the Channel List and Channel Messages, then I need a way to reflect the membership change back to this context in order for the buttons to switch.

For example, this is what my subscribe action looks like:

const subscribe = async () => {
  await channel.addMembers([user.id]);

  // Fetch the latest channel data and state
  const [nextChannel] = await client.queryChannels({
    cid: channel.cid,
  });

  if (nextChannel) {
    // Update the context so that the channel new membership is reflected
    setChannel(nextChannel);
  }
}

It seems that React ignores the setChannel(nextChannel) call because it deems there to be no change between channel and nextChannel in this case (due to shallow comparison).

So what I've had to do is add a hack to force the screen to re-render by extending the context such that I can force a re-render:

setChannel(nextChannel);
// Update the same context as above
setForceUpdate((prev) => prev + 1);

Is there a recommended way to do this that I'm missing, without needing this force update workaround?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions