Javascript 如何递归地呈现react.js中的子组件

Javascript 如何递归地呈现react.js中的子组件,javascript,recursion,reactjs,Javascript,Recursion,Reactjs,我想从自己的组件中递归地添加一个react组件。我看到了一个示例,它通过子树节点进行映射,并以相同的方式添加子节点。不幸的是,这对我根本不起作用。想法是有一个简单的评论组件,回复将重用相同的组件 var Comment = React.createClass({ render: function() { return ( <div className="comment"> {/* text and author */}

我想从自己的组件中递归地添加一个react组件。我看到了一个示例,它通过子树节点进行映射,并以相同的方式添加子节点。不幸的是,这对我根本不起作用。想法是有一个简单的评论组件,回复将重用相同的组件

var Comment = React.createClass({
  render: function() {    
    return (
        <div className="comment">

          {/* text and author */}
          <div className="comment-text">
            <span className="author">{this.props.author}</span>         
            <span className="body" dangerouslySetInnerHTML={{__html: this.props.body}} />
          </div>

          {/* replies */}
          <div className="replies">
           {
             this.props.replies.map(function(reply) {
               <Comment body={reply.body} author={reply.author} />
             }.bind(this))
          }
          </div>

      </div>
    );
  }
});
{ "author" : "Some user",
  "body" : "<div>Great work</div>",
  "replies" : [ { "author" : "A user replying",
        "body" : "<div Yes it was great work</div>"
      },
      { "author" : "Another user replying",
        "body" : "<div It really was great work!</div>"
      }
    ]
}
var Comment=React.createClass({
render:function(){
返回(
{/*文本和作者*/}
{this.props.author}
{/*答复*/}
{
this.props.reply.map(函数(reply){
}.绑定(本)
}
);
}
});
我收到以下错误消息:

未捕获的TypeError:未能构造“Comment”:请使用“new”运算符,此DOM对象构造函数不能作为函数调用

下面是传递给组件的JSON数据示例

var Comment = React.createClass({
  render: function() {    
    return (
        <div className="comment">

          {/* text and author */}
          <div className="comment-text">
            <span className="author">{this.props.author}</span>         
            <span className="body" dangerouslySetInnerHTML={{__html: this.props.body}} />
          </div>

          {/* replies */}
          <div className="replies">
           {
             this.props.replies.map(function(reply) {
               <Comment body={reply.body} author={reply.author} />
             }.bind(this))
          }
          </div>

      </div>
    );
  }
});
{ "author" : "Some user",
  "body" : "<div>Great work</div>",
  "replies" : [ { "author" : "A user replying",
        "body" : "<div Yes it was great work</div>"
      },
      { "author" : "Another user replying",
        "body" : "<div It really was great work!</div>"
      }
    ]
}
{“作者”:“某个用户”,
“身体”:“伟大的工作”,
“回复”:[{“作者”:“回复的用户”,

“body”:“如果我在render方法的顶部创建子节点作为对象,它可以正常工作

export default class extends React.Component {
  let replies = null
  if(this.props.replies){
    replies = this.props.replies.map((reply) => {
      return (
        <Comment author={reply.author} body={reply.body} />
      )
    })
  }

  render() {
    return (
      <div className="comment">
        <div className="replies">{ replies }</div>
      </div>
    )
  }
}
导出默认类扩展React.Component{
让回复=null
如果(此.props.repress){
回复=this.props.replets.map((回复)=>{
返回(
)
})
}
render(){
返回(
{答复}
)
}
}

在ES6中有一个替代方案:

import React, { Component, PropTypes } from 'react'

export default class Comments extends Component {

  render() {

    const { children } = this.props

    return (
      <div className="comments">
        {children.map(comment =>
          <div key={comment.id} className="comment">
            <span>{comment.content}</span>
            {comment.children && <Comments children={comment.children}/>}
          </div>
        )}
      </div>
    )

  }

}

Comments.propTypes = {
  children: PropTypes.array.isRequired
}
import React,{Component,PropTypes}来自“React”
导出默认类注释扩展组件{
render(){
const{children}=this.props
返回(
{children.map(注释=>
{comment.content}
{comment.children&&}
)}
)
}
}
Comments.propTypes={
子项:需要PropTypes.array.isRequired
}
还有一些其他成分:

<Comments children={post.comments}/>

最简单的方法是在类中创建一个函数,返回类的实例:

RecursiveComponent.rt.js:

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

});
<div>
  ...
  <div rt-repeat="recursiveChild in this.props.recursiveItem.recursiveChilds">
            {this.renderRecursive(recursiveChild)}
  </div>
</div>
如果您使用:

RecursiveComponent.rt:

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

});
<div>
  ...
  <div rt-repeat="recursiveChild in this.props.recursiveItem.recursiveChilds">
            {this.renderRecursive(recursiveChild)}
  </div>
</div>

...
{this.renderCursive(recursiveChild)}

您在
repress.map
函数中缺少了
return
。从错误消息中可以看出,问题在于如何创建顶级评论,无论您在哪里使用它。多亏了这一点,这可能是问题所在,我现在将其更改为下面,设置了一个repress对象,然后它也可以正常工作。这s在alltree中不是递归的组件链接不起作用这里的递归在哪里?如果组件有子级,它会呈现自身