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: Ensure unique keys for comments in ResourceCommentSection #11125

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/components/Resource/ResourceCommentSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,13 @@ const CommentSection = (props: { id: string }) => {
) : (
<ul>
{resourceComments?.results
? [...resourceComments.results].reverse().map((comment) => (
<li key={comment.id} className="w-full">
<Comment {...comment} />
</li>
))
? [...resourceComments.results]
.reverse()
.map((comment, index) => (
<li key={comment.id || index} className="w-full">
Copy link
Contributor

Choose a reason for hiding this comment

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

🤔 What exactly is this addressing? It shouldn't be causing issues since comments will always have a unique Id.

Copy link
Author

Choose a reason for hiding this comment

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

This ensures that if at any point comment.id is null or undefined it will fallback to another unique key called index rather than using undefined, which could cause rendering issues in React.

Copy link
Contributor

Choose a reason for hiding this comment

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

This ensures that if at any point comment.id is null or undefined it will fallback to another unique key called index rather than using undefined, which could cause rendering issues in React.

In the first place will a comments id ever be null!?

Copy link
Author

Choose a reason for hiding this comment

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

If comment.id is always there, we don’t need a fallback. But in some (rare) cases, like when data is still loading
or API Failures, id could be null or undefined, which might cause React rendering issues.
This fallback ensures stability for such cases.

<Comment {...comment} />
</li>
))
: null}
<div className="flex w-full items-center justify-center">
<div
Expand Down