当前位置:网站首页>Antd comment recursive loop comment
Antd comment recursive loop comment
2022-07-07 04:02:00 【Recall @y】
One 、 Business scenario :
Recently in use umi and Antd Development , It's doing Multi level nested comments When I met some pits , For everyone to meet the same problem as me later , Let me share with you
Bold style Two 、 Solution :
First define the function component , Return to the page structure to render . Then write the main function , Write a method that recursively calls components , And loop through the previous function components , Call multiple times through judgment , Finally, the recursive component is returned and rendered on the page
3、 ... and 、 Specific implementation steps
1、 Define function components , Return to the page structure to render
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、 Write the main function , Write a method that recursively calls components , And loop through the previous function components , Call multiple times through judgment , Finally, the recursive component is returned and rendered on the page
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、 The complete code is as follows
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: ' I'm a fool , uppercase ',
children: [
{
id: 11,
name: ' I'm a fool , uppercase ',
children: [{
id: 111,
name: ' I'm a fool , uppercase ',
}]
},
{
id: 12,
name: ' I'm a fool , uppercase ',
children: [{
id: 121,
name: ' I'm a fool , uppercase ',
}]
},
]
},
{
id: 12,
name: ' I'm a fool , uppercase ',
children: [
{
id: 121,
name: ' I'm a fool , uppercase ',
children: [{
id: 1211,
name: ' I'm a fool , uppercase ',
}]
},
{
id: 212,
name: ' I'm a fool , uppercase ',
children: [{
id: 1221,
name: ' I'm a fool , uppercase ',
}]
},
]
}
]
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;
Four 、 Effect display
This is the end of today's sharing , Welcome friends to communicate together
边栏推荐
- Probability formula
- Que savez - vous de la sérialisation et de l'anti - séquence?
- Kalman filter-1
- [MySQL] row sorting in MySQL
- Delete data in SQL
- AVL树插入操作与验证操作的简单实现
- codeforces每日5题(均1700)-第七天
- Ggplot facet detail adjustment summary
- opencv第三方库
- Termux set up the computer to connect to the mobile phone. (knock the command quickly), mobile phone termux port 8022
猜你喜欢
随机推荐
ABAP 动态内表分组循环
Introduction to opensea platform developed by NFT trading platform (I)
Search of linear table
本机mysql
使用 BR 恢复 GCS 上的备份数据
The most complete learning rate adjustment strategy in history LR_ scheduler
[MySQL] row sorting in MySQL
【OA】Excel 文档生成器: Openpyxl 模块
Top 50 hit industry in the first half of 2022
中青杯2022A题高校数学建模竞赛与课程教育思路分析
Enter the rough outline of the URL question (continuously updated)
Mysql-数据丢失,分析binlog日志文件
Unity3D在一建筑GL材料可以改变颜色和显示样本
QT 打开文件 使用 QFileDialog 获取文件名称、内容等
使用Thread类和Runnable接口实现多线程的区别
二进制、八进制、十六进制
Arduino droplet detection
2022中青杯C题城市交通思路分析
Summer 2022 daily question 1 (1)
【安全攻防】序列化与反序列,你了解多少?