0

We use Sendbird for chat. I have message component with buttons that run addReaction method enter image description here. And according to the docs https://sendbird.com/docs/chat/v3/javascript/guides/group-channel#2-react-to-a-message I need to do message.applyReactionEvent(reactionEvent) but it applies not ‘in live’

In main Chat component I added onReactionUpdated and it runs after reaction click

enter image description here

But reaction reveals only if I reload page or close dialog and open again (in general, after update of whole dialog). I guess that there are mistakes in my code but can’t figure out what actually wrong.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Can you please paste the code you are using in the question so it is easier for others to test it out? Most better, you could create a code sandbox using https://codesandbox.io and paste the link here. – Harry Theo Dec 15 '21 at 19:00
  • Hello Theo. I guess it's you answered me at Sendbird community forum) So I'll just past my answer and here if someone will ever google this problem – Appolinaris Dec 17 '21 at 21:20
  • Please provide enough code so others can better understand or reproduce the problem. – Community Dec 22 '21 at 17:17

1 Answers1

0

The problem was in updates react components. So I solved it by updating message component after reaction click using then(). Small explanation to code - for addReaction event I have setState that close pop-up with emojis and updates my component. For deleteReaction I don’t have any function that would fit there so I decided to use kind of forceUpdate from React docs

const [_, forceUpdate] = useReducer(x => x + 1, 0);

And my function:

const toggleReaction = (type, emojiKey) => {
if (type === "add") {
  channel
    .addReaction(message, emojiKey, function (reactionEvent, error) {
      if (error) {
        console.log(error);
        return setShowEmojis(false);
      }
      message.applyReactionEvent(reactionEvent);
    })
    .then(() => setShowEmojis(false));
}
if (type === "delete") {
  channel
    .deleteReaction(message, emojiKey, function (reactionEvent, error) {
      message.applyReactionEvent(reactionEvent);
    })
    .then(() => forceUpdate());
}
};