Nested comments in Reactjs

后端 未结 2 1989
慢半拍i
慢半拍i 2021-02-10 20:50

I have the following json:

{  
  \"comments\":[  
    {  
      \"id\":1,
      \"comment_text\":\"asdasdadasdsadsadadsa\",
      \"author\":\"adsfasdasdsad\",
          


        
2条回答
  •  死守一世寂寞
    2021-02-10 21:03

    You need two components: Comments and Comment.

    Comment = React.createClass({
      render: function(){
        var comment = this.props.comment;
        return 

    {comment.author} says {comment.comment_text}

    } }); Comments = React.createClass({ render: function(){ return
    {this.props.comments.map(function(comment){ return })
    } });

    The Comment renders Comments, which in turn can render Comment nodes, etc. This recursively builds the comment structure.

提交回复
热议问题