Rendering nested/threaded comments in React

前端 未结 2 1514
隐瞒了意图╮
隐瞒了意图╮ 2021-02-06 11:35

Given the below array, I\'d like to render comments in a threaded manner by making use of parentId.

comments: [
    {
      id: 1,
            


        
2条回答
  •  无人共我
    2021-02-06 11:46

    If you turn that list into a structure which actually reflects the nested hierarchy of the comments, then you'll have an easier time building a component for rendering them.

    [
      {
        id: 1,
        children: [
          { id: 2, children: [] },
          { id: 3, children: [ ... ] }
        ]
      }
    ]
    

    You could implement a function to do the conversion.

    function nestComments(commentList) {
      const commentMap = {};
    
      // move all the comments into a map of id => comment
      commentList.forEach(comment => commentMap[comment.id] = comment);
    
      // iterate over the comments again and correctly nest the children
      commentList.forEach(comment => {
        if(comment.parentId !== null) {
          const parent = commentMap[comment.parentId];
          (parent.children = parent.children || []).push(comment);
        }
      });
    
      // filter the list to return a list of correctly nested comments
      return commentList.filter(comment => {
        return comment.parentId === null;
      });
    }
    

    Here's an idea for how you could go from that flat structure to a list of nested comments. Once you're done with that implementation, all you'd need would be a recursive React component.

    function Comment({ comment }) {
      const nestedComments = (comment.children || []).map(comment => {
        return ;
      });
    
      return (
        
    {comment.text} {comment.author.name} {nestedComments}
    ); }

提交回复
热议问题