Skip to content

Instantly share code, notes, and snippets.

@dglowinski
Created July 14, 2017 11:49
Show Gist options
  • Save dglowinski/c49b360c1a991e0055243967f44a2e37 to your computer and use it in GitHub Desktop.
Save dglowinski/c49b360c1a991e0055243967f44a2e37 to your computer and use it in GitHub Desktop.
React - children prop as callback
// Calls the children callback numTimes to produce a repeated component
function Repeat(props) {
let items = [];
for (let i = 0; i < props.numTimes; i++) {
items.push(props.children(i));
}
return <div>{items}</div>;
}
function ListOfTenThings() {
return (
<Repeat numTimes={10}>
{(index) => <div key={index}>This is item {index} in the list</div>}
</Repeat>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment