-
-
Save wguilherme/8c1b6721a532cb25319f95a47b3520fa to your computer and use it in GitHub Desktop.
react repeat component
This file contains hidden or 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
/** | |
* usage: | |
* <ul> | |
* <Repeat times="3"> | |
* <li>item</li> | |
* </Repeat> | |
* </ul> | |
* or | |
* <ul> | |
* <Repeat times="3"> | |
* {i => <li key={i}>item {i}</li>} | |
* </Repeat> | |
* </ul> | |
*/ | |
function Repeat({ children, times }) { | |
const _times = typeof times === "number" ? times : parseInt(times); | |
return new Array(_times).fill().map((_, i) => typeof children === "function" ? children(i) : children); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment