Antd Comment 递归循环评论

一、业务场景:
最近在使用umi和Antd 进行开发,其中在做多级嵌套评论的时候遇到了一些坑,为了大家后面遇到和我一样的问题,给大家分享一下

加粗样式二、解决方案:
先定义函数组件,返回要渲染的页面结构。再书写主函数,写一个递归调用组件的方法,并对之前的函数组件进行循环遍历,通过判断多次调用,最后返回递归组件渲染到页面上

三、具体实现步骤

1、定义函数组件,返回要渲染的页面结构

import { ReactElement, useCallback } from 'react';
import { Avatar, Comment } from 'antd';

const ExampleComment = (props) => {
  console.log(props)
  const { children, _node: { name } } = props;
  return <Comment
    actions={[<span key="comment-nested-reply-to">Reply to</span>]}
    author={<a>Han Solo</a>}
    avatar={<Avatar src="https://joeschmoe.io/api/v1/random" alt="Han Solo" />}
    content={
      <p>
        {name}
      </p>
    }
  >
    {children}
  </Comment>
};

2、书写主函数,写一个递归调用组件的方法,并对之前的函数组件进行循环遍历,通过判断多次调用,最后返回递归组件渲染到页面上

function notificationrecord(): ReactElement {
  const renderTreeNodes = useCallback(
    (node): ReactElement[] => {
      if (!node) return [];
      return node.map((_node) => (
        <ExampleComment key={_node.id} _node={_node}>
          {renderTreeNodes(_node.children)}
        </ExampleComment>
      ));
    },
    [],
  );

  return (
    <>
      {
        renderTreeNodes(data)
      }
    </>
  );
}
export default notificationrecord;

3、完整代码如下

import { ReactElement, useCallback } from 'react';
import { Avatar, Comment } from 'antd';

const ExampleComment = (props) => {
  console.log(props)
  const { children, _node: { name } } = props;
  return <Comment
    actions={[<span key="comment-nested-reply-to">Reply to</span>]}
    author={<a>Han Solo</a>}
    avatar={<Avatar src="https://joeschmoe.io/api/v1/random" alt="Han Solo" />}
    content={
      <p>
        {name}
      </p>
    }
  >
    {children}
  </Comment>
};

function notificationrecord(): ReactElement {
  const data = [
    {
      id: 1,
      name: '我是个笨蛋,大写的',
      children: [
        {
          id: 11,
          name: '我是个笨蛋,大写的',
          children: [{
            id: 111,
            name: '我是个笨蛋,大写的',
          }]
        },
        {
          id: 12,
          name: '我是个笨蛋,大写的',
          children: [{
            id: 121,
            name: '我是个笨蛋,大写的',
          }]
        },
      ]
    },
    {
      id: 12,
      name: '我是个笨蛋,大写的',
      children: [
        {
          id: 121,
          name: '我是个笨蛋,大写的',
          children: [{
            id: 1211,
            name: '我是个笨蛋,大写的',
          }]
        },
        {
          id: 212,
          name: '我是个笨蛋,大写的',
          children: [{
            id: 1221,
            name: '我是个笨蛋,大写的',
          }]
        },
      ]
    }
  ]
  const renderTreeNodes = useCallback(
    (node): ReactElement[] => {
      if (!node) return [];
      return node.map((_node) => (
        <ExampleComment key={_node.id} _node={_node}>
          {renderTreeNodes(_node.children)}
        </ExampleComment>
      ));
    },
    [],
  );

  return (
    <>
      {
        renderTreeNodes(data)
      }
    </>
  );
}
export default notificationrecord;

四、效果展示
在这里插入图片描述

今天的分享到此结束,欢迎小伙伴们一起交流

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值