Last active
January 10, 2024 10:37
-
-
Save ViliamKopecky/a212e5a8983f10b60665241b6ffc8ef5 to your computer and use it in GitHub Desktop.
Array.join for React children
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
))} | |
</> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Co já vím, tak v novějším Reactu už je legit vracet z komponent rovnou pole.
Dříve
Nyní