how to render child components in react.js recursively

前端 未结 3 1169
渐次进展
渐次进展 2020-12-07 22:46

I wanted to recursively add a react component from within its own component. I saw this example of a tree component which was mapping through the child TreeNodes and adding

3条回答
  •  攒了一身酷
    2020-12-07 23:39

    The easiest way is to create a function in the class which returns an instance of your class:

    RecursiveComponent.rt.js:

    var RecursiveComponent = React.createClass({
     render: function() {
      // JSX
      ....
     },
     renderRecursive: function(param1)
       return React.createElement(RecursiveComponent, {param1: param1});
    
    });
    

    if you use react-templates library:

    RecursiveComponent.rt:

    ...
    {this.renderRecursive(recursiveChild)}

提交回复
热议问题