当前位置:网站首页>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
边栏推荐
- What is Ba? How about Ba? What is the relationship between Ba and Bi?
- map和set的实现
- idea gradle lombok 报错集锦
- QT 项目 表格新建列名称设置 需求练习(找数组消失的数字、最大值)
- 数据的存储
- Class常量池与运行时常量池
- Tflite model transformation and quantification
- ERROR: Could not build wheels for pycocotools which use PEP 517 and cannot be installed directly
- Implementation of binary search tree
- leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
猜你喜欢

How to detect whether the MySQL code runs deadlock +binlog view

Clock in during winter vacation

codeforces每日5题(均1700)-第七天

预处理——插值

Some common software related

Redis configuration and optimization of NoSQL

Mobile measurement and depth link platform - Branch

Codeworks 5 questions per day (1700 average) - day 7

Que savez - vous de la sérialisation et de l'anti - séquence?

1.19.11.SQL客户端、启动SQL客户端、执行SQL查询、环境配置文件、重启策略、自定义函数(User-defined Functions)、构造函数参数
随机推荐
PHP implements lottery according to probability
运算放大器应用汇总1
ggplot 分面的细节调整汇总
概率论公式
Que savez - vous de la sérialisation et de l'anti - séquence?
Collection of idea gradle Lombok errors
Codeworks 5 questions per day (1700 average) - day 7
手机号国际区号JSON格式另附PHP获取
Vernacular high concurrency (2)
Kotlin Android 环境搭建
First understand the principle of network
VHDL implementation of single cycle CPU design
2022年电工杯B 题 5G 网络环境下应急物资配送问题思路分析
SSL certificate deployment
Search of linear table
MySQL的索引
ABAP dynamic inner table grouping cycle
NoSQL之Redis配置与优化
维护万星开源向量数据库是什么体验
Baidu map JS development, open a blank, bmapgl is not defined, err_ FILE_ NOT_ FOUND