如何在 react.js 中递归渲染子组件 [英] how to render child components in react.js recursively

查看:55
本文介绍了如何在 react.js 中递归渲染子组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

var Comment = React.createClass({渲染:函数(){返回 (<div className="评论">{/* 文本和作者 */}<div className="注释文本"><span className="author">{this.props.author}</span><span className="body" risklySetInnerHTML={{__html: this.props.body}}/>

{/* 回复 */}<div className="回复">{this.props.reply.map(function(reply) {<评论正文={reply.body} author={reply.author}/>}.bind(this))}

);}});

我收到以下错误消息:

未捕获的类型错误:无法构造注释":请使用新"运算符,此 DOM 对象构造函数不能作为函数调用.

这是传递给组件的 JSON 数据的示例.

{ "author" : "某个用户","body" : "

很棒的作品

",回复":[ {作者":用户回复","body" : "<div 是的,这很棒</div>"},{ "author" : "另一个用户正在回复","body" : "<div 真的很棒!</div>"}]}

解决方案

如果我将子节点创建为渲染方法顶部的对象,它工作正常.

export default class extends React.Component {让回复 = null如果(this.props.replys){回复 = this.props.reply.map((reply) => {返回 (<评论作者={reply.author} body={reply.body}/>)})}使成为() {返回 (<div className="评论"><div className="replys">{replys }</div>

)}}

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 child nodes in the same way. Unfortunately it doesn't work at all for me. The idea was to have a simple comment component, and the replies would reuse the same component.

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>
    );
  }
});

I get the following error message:

Uncaught TypeError: Failed to construct 'Comment': Please use the 'new' operator, this DOM object constructor cannot be called as a function.

here is an example of the JSON data passed to the component.

{ "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>"
      }
    ]
}

解决方案

If I create the child nodes as an object at the top of the render method, it works fine.

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.js 中递归渲染子组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆