Skip to content

Instantly share code, notes, and snippets.

@ViliamKopecky
Last active January 10, 2024 10:37
Show Gist options
  • Save ViliamKopecky/a212e5a8983f10b60665241b6ffc8ef5 to your computer and use it in GitHub Desktop.
Save ViliamKopecky/a212e5a8983f10b60665241b6ffc8ef5 to your computer and use it in GitHub Desktop.
Array.join for React children
import React from 'react'
export function Join(props: { items: Array<React.ReactNode>; glue?: React.ReactNode; lastGlue?: React.ReactNode }) {
const glue = props.glue ?? ', '
const last = props.lastGlue ?? glue
return (
<>
{props.items.map((item, i, list) => (
<React.Fragment key={i}>
{i > 0 && <React.Fragment key={-i}>{i === list.length - 1 ? last : glue}</React.Fragment>}
{item}
</React.Fragment>
))}
</>
)
}
@FilipChalupa
Copy link

Co já vím, tak v novějším Reactu už je legit vracet z komponent rovnou pole.

Dříve

image

Nyní

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment