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] Re-rendering fixed while opening pinned and starred messages. #644

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

Barrylimarti
Copy link

Brief Title

While opening starred messages, useSetMessageList hook was getting re-rendered many infinitely.

Acceptance Criteria fulfillment

  • Stop the re-rendering while keeping the functionality same.

Fixes #642

Video/Screenshots

Previously:

error.mov

Now:

Screen.Recording.2024-10-26.at.1.57.02.PM.mov

PR Test Details

Added memoization to stop the infinite re-render cycle.

Note: The PR will be ready for live testing at https://rocketchat.github.io/EmbeddedChat/pulls/pr-<pr_number> after approval.

@CLAassistant
Copy link

CLAassistant commented Oct 26, 2024

CLA assistant check
All committers have signed the CLA.

@Spiral-Memory
Copy link
Collaborator

Hey @Barrylimarti ,

Thanks for raising the PR. Could you please let me know why this issue is happening? I believe that even in the useEffect, the dependencies are already defined, so it should only run when the dependencies change.

What specifically is causing this to run infinitely? The last time I tested, it was working fine, but since some new PRs were merged, this issue may have been introduced.

I don't have my laptop with me right now, so please help me understand the issue.

@Barrylimarti
Copy link
Author

Hey @Spiral-Memory,
As far as I investigated, inside the hook while setting up the message list, apparently the messages array re-rendered everytime. I haven't really dug that deep in this but it maybe due to the component re-rendering itself due to a change or something else.
I think either way it is a good practice to memoize the dependencies and functions to prevent any case like this in future.

Copy link
Collaborator

@Spiral-Memory Spiral-Memory left a comment

Choose a reason for hiding this comment

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

Code LGTM !
Approving to test

@Spiral-Memory
Copy link
Collaborator

Hey @Spiral-Memory,
As far as I investigated, inside the hook while setting up the message list, apparently the messages array re-rendered everytime. I haven't really dug that deep in this but it maybe due to the component re-rendering itself due to a change or something else.
I think either way it is a good practice to memoize the dependencies and functions to prevent any case like this in future.

If possible, i would request you to deep dive into it to figure out the actual reason why exactly this is happening. Thanks 🙏

@Barrylimarti
Copy link
Author

Sure will definitely check this out!

@Barrylimarti
Copy link
Author

Hey @Spiral-Memory, I investigated this further and found that PR #628 was the breaking change.
In MessageAggregator,
When allMessages was created with [...messages, ...threadMessages], a new array reference is being created on every render. This means that even if the contents of messages and threadMessages haven't changed, allMessages will be a new object each time.
Thus, causing the useSetMessageList hook to re-render.
I tried memoizing allMessages also, and it worked but I guess doing it inside the hook is much better as it will prevent further errors like this.

@Spiral-Memory
Copy link
Collaborator

Spiral-Memory commented Oct 26, 2024

Thanks for the detailed information, @Barrylimarti .

I really appreciate it! Based on the current situation, please make the necessary changes to resolve the root cause of the issue.

Well, i have one more doubt, even if there is no action or render, why is new array is being created infinitely ? Is it something that it is setting some state and that state is causing a re-render ?

Let me know once everything looks good to you, and I’ll review it.

Thanks again!

@Barrylimarti
Copy link
Author

Thanks for the detailed information, @Barrylimarti .

I really appreciate it! Based on the current situation, please make the necessary changes to resolve the root cause of the issue.

Well, i have one more doubt, even if there is no action or render, why is new array is being created infinitely ? Is it something that it is setting some state and that state is causing a re-render ?

Let me know once everything looks good to you, and I’ll review it.

Thanks again!

Yes you are right @Spiral-Memory, hook sets a state which triggers the re-render of the whole component, which in turn creates the allMessages array again even though there's no change in messages or threadMessages because of the spread operator. Thus, triggering the hook again.

I am pushing couple of more changes as a safety measure for this situation in some time. Please review and let me know! Thanks.

@Barrylimarti
Copy link
Author

I have pushed all the changes required @Spiral-Memory. You can review it now.

@Barrylimarti
Copy link
Author

Missed some imports and linting errors. Fixed it in the latest commit.

@devanshkansagra
Copy link
Contributor

Hey @Barrylimarti, The pin messages sidebar is crashing might be due to your changes in the your deployed pr: https://rocketchat.github.io/EmbeddedChat/pulls/pr-644

Screencast.from.28-10-24.10.55.54.AM.IST.webm

Could you please check this out? why this is occuring

also check in your localhost

@devanshkansagra
Copy link
Contributor

devanshkansagra commented Oct 28, 2024

Same goes with starred messages as well

Screencast.from.28-10-24.11.09.19.AM.IST.webm

@Barrylimarti
Copy link
Author

Hi @devanshkansagra, I checked the code on my localhost it is working perfectly fine. These were some import error that I missed in a commit and it was fixed in this commit.

I am not sure why this is not showing in the deployed preview. Maybe re-triggering build can fix this? Can you please check?

@devanshkansagra
Copy link
Contributor

@Spiral-Memory, can you re-approve the changes?

@devanshkansagra
Copy link
Contributor

might be the latest changes were not deployed

@devanshkansagra
Copy link
Contributor

yep now working well

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.

Bug: useSetMessageList re-rendering when starred and pinned messages is opened
4 participants