Skip to content

Instantly share code, notes, and snippets.

@wguilherme
Forked from mskoroglu/Repeat.js
Created December 6, 2020 02:46
Show Gist options
  • Save wguilherme/8c1b6721a532cb25319f95a47b3520fa to your computer and use it in GitHub Desktop.
Save wguilherme/8c1b6721a532cb25319f95a47b3520fa to your computer and use it in GitHub Desktop.
react repeat component
/**
* 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