当前位置:网站首页>Antd Comment 递归循环评论
Antd Comment 递归循环评论
2022-07-06 21:09:00 【拾忆@y】
一、业务场景:
最近在使用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;
四、效果展示
今天的分享到此结束,欢迎小伙伴们一起交流
边栏推荐
- Free PHP online decryption tool source code v1.2
- When QT uses qtooltip mouse to display text, the picture of the button will also be displayed and the prompt text style will be modified
- [leetcode] 450 and 98 (deletion and verification of binary search tree)
- 24. (ArcGIS API for JS) ArcGIS API for JS point modification point editing (sketchviewmodel)
- About Estimation Statistics
- Hisilicon 3559 universal platform construction: RTSP real-time playback support
- QT 项目 表格新建列名称设置 需求练习(找数组消失的数字、最大值)
- 【安全攻防】序列化與反序列,你了解多少?
- 接口数据安全保证的10种方式
- C task expansion method
猜你喜欢

21. (article ArcGIS API for JS) ArcGIS API for JS rectangular acquisition (sketchviewmodel)

GPT-3当一作自己研究自己,已投稿,在线蹲一个同行评议
![[hcie TAC] question 3](/img/d2/38a7286be7780a60f5311b2fcfaf0b.jpg)
[hcie TAC] question 3
Docker部署Mysql8的实现步骤

ubuntu20安裝redisjson記錄

Force buckle ----- path sum III

About Confidence Intervals

Ubuntu 20 installation des enregistrements redisjson

Open3d mesh filtering
![[development software] tilipa Developer Software](/img/b8/de2a1ea6474bb3f9b44e7ea01c441b.png)
[development software] tilipa Developer Software
随机推荐
Sub pixel corner detection opencv cornersubpix
The true face of function pointer in single chip microcomputer and the operation of callback function
MySQL的存储引擎
AVL树插入操作与验证操作的简单实现
ubuntu20安裝redisjson記錄
Kotlin Android 环境搭建
On file uploading of network security
预处理——插值
【mysql】mysql中行排序
华为小米互“抄作业”
It's too convenient. You can complete the code release and approval by nailing it!
Tencent cloud native database tdsql-c was selected into the cloud native product catalog of the Academy of communications and communications
如何检测mysql代码运行是否出现死锁+binlog查看
web服务性能监控方案
Kalman filter-1
leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
[leetcode] 700 and 701 (search and insert of binary search tree)
维护万星开源向量数据库是什么体验
ubuntu20安装redisjson记录
Force buckle ----- path sum III