Skip to content

Instantly share code, notes, and snippets.

@mskoroglu
Last active April 20, 2022 08:26
Show Gist options
  • Save mskoroglu/39e99f08218be178503a97d1ef544690 to your computer and use it in GitHub Desktop.
Save mskoroglu/39e99f08218be178503a97d1ef544690 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