Skip to content

Instantly share code, notes, and snippets.

@byurhannurula
Forked from mskoroglu/Repeat.js
Created October 15, 2019 14:58
Show Gist options
  • Save byurhannurula/1730e18024f0e3487e95151bc39802fd to your computer and use it in GitHub Desktop.
Save byurhannurula/1730e18024f0e3487e95151bc39802fd 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}
* 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