Created
July 14, 2017 11:49
-
-
Save dglowinski/c49b360c1a991e0055243967f44a2e37 to your computer and use it in GitHub Desktop.
React - children prop as callback
This file contains hidden or 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
// 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