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

feat: improve performance using uuid in key instead index array position #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Exodiel
Copy link

@Exodiel Exodiel commented Sep 29, 2023

No description provided.

@vercel
Copy link

vercel bot commented Sep 29, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-carousel ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 29, 2023 3:46am

Copy link
Member

@JavierCane JavierCane left a 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)
Copy link
Member

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?

Copy link
Author

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">
Copy link
Member

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?

Copy link
Author

@Exodiel Exodiel Sep 30, 2023

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

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.

2 participants