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