Want to render React Component into itself, in a recursive way? You should abstract your rendering logic into a separate method, which you can call recursively from the “render” method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import List from 'somewhere'; class RecursiveItems extends React.PureComponent { constructListItem = (item) => { if (item.nestedItems) { return ( <List key={item.key}> {item.nestedItems.map(this.constructListItem)} </List> ) } else { return <ListItem key={item.key} item={item} /> } } render() { const { listItems } = this.props return ( <List> {listItems.map(this.constructListItem)} </List> ) } } export default RecursiveItems |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.