-
Notifications
You must be signed in to change notification settings - Fork 16
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
Fixed using array index as key in lists #2685
Conversation
@@ -214,7 +214,7 @@ const AddProfessionalCategoryModal: FC<AddProfessionalCategoryModalProps> = ({ | |||
disableOptions={data.subjects as Array<Partial<SubjectInterface>>} | |||
handleChange={handleProfessionalSubjectChange(index)} | |||
handleSubjectDelete={() => handleSubjectDelete(subject._id || '')} | |||
key={index} | |||
key={subject._id || crypto.randomUUID()} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any chance for _id
here to be falsable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, sometimes it's just an empty string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can remove crypto.randomUUID()
, since id=''
will only be used once, when no category has been selected yet.
@@ -214,7 +214,7 @@ const AddProfessionalCategoryModal: FC<AddProfessionalCategoryModalProps> = ({ | |||
disableOptions={data.subjects as Array<Partial<SubjectInterface>>} | |||
handleChange={handleProfessionalSubjectChange(index)} | |||
handleSubjectDelete={() => handleSubjectDelete(subject._id || '')} | |||
key={index} | |||
key={subject._id || crypto.randomUUID()} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can remove crypto.randomUUID()
, since id=''
will only be used once, when no category has been selected yet.
Quality Gate passedIssues Measures |
Description:
Using array indices as keys in list components can cause unexpected behavior, especially when items are reordered or changed. Instead, use a unique ID for each item to help React handle updates more efficiently.
File Affected:
AddProfessionalCategoryModal.tsx