Last active
April 20, 2022 08:26
-
-
Save mskoroglu/39e99f08218be178503a97d1ef544690 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