-
Notifications
You must be signed in to change notification settings - Fork 3
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
feat: improve performance using uuid in key instead index array position #3
base: main
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
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.
Hi!
Thanks for your contribution. Using the array index as the component key is not a best practice definitely, however, I have some doubts regarding the proposed alternative. I have left some comments regarding these doubts in order to discuss them 😊
Thanks!!
@@ -43,11 +43,17 @@ export function Carousel({ | |||
scrollSliderPrevious(slider); | |||
} | |||
|
|||
function uuid() { | |||
return ( | |||
Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15) |
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.
Have you considered using crypto.randomUUID? Does it has any downside for this purpose?
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.
Of course, my first option was that, but maybe in local develop crypto web api will not work because only runs in secure contexts (https)
{children.map((child, index) => ( | ||
<div key={index} className="carousel__slide"> | ||
{children.map((child) => ( | ||
<div key={uuid()} className="carousel__slide"> |
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.
Based on this, should not we avoid calling the uuid()
function while returning the component?
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.
you're right, in this case we could use a simple implementation, like this:
{children.map((child, index) => (
<div key={`carouselItem${index}`} className="carousel__slide">
{child}
</div>
))}
interpolate the index list with a generic string
No description provided.