Skip to content

feat: improve performance using uuid in key instead index array position #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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions src/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)

);
}

return (
<div className="carousel">
<div ref={slider} className="carousel__slider">
{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

{child}
</div>
))}
Expand Down