当前位置:网站首页>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
边栏推荐
- 复杂因子计算优化案例:深度不平衡、买卖压力指标、波动率计算
- Some common software related
- Preprocessing - interpolation
- termux设置电脑连接手机。(敲打命令贼快),手机termux端口8022
- 【安全攻防】序列化与反序列,你了解多少?
- tflite模型转换和量化
- Do you choose pandas or SQL for the top 1 of data analysis in your mind?
- Introduction to opensea platform developed by NFT trading platform (I)
- NoSQL之Redis配置与优化
- 使用 TiDB Lightning 恢复 GCS 上的备份数据
猜你喜欢

Some thoughts on cross end development of kbone and applet

Arduino droplet detection

R data analysis: how to predict Cox model and reproduce high score articles

自适应非欧表征广告检索系统AMCAD

太方便了,钉钉上就可完成代码发布审批啦!

web服务性能监控方案

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

Hisilicon 3559 universal platform construction: RTSP real-time playback support

Antd Comment 递归循环评论

【安全攻防】序列化与反序列,你了解多少?
随机推荐
API data interface of A-share index component data
SSL certificate deployment
Kbone与小程序跨端开发的一些思考
使用切面实现记录操作日志
golang 根据生日计算星座和属相
使用 BR 恢复 GCS 上的备份数据
Vernacular high concurrency (2)
链表面试常见题
Preprocessing - interpolation
Ggplot facet detail adjustment summary
leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
Que savez - vous de la sérialisation et de l'anti - séquence?
R data analysis: how to predict Cox model and reproduce high score articles
ABAP Dynamic Inner table Group cycle
Arduino droplet detection
Class constant pool and runtime constant pool
史上最全学习率调整策略lr_scheduler
QT thread and other 01 concepts
GPT-3当一作自己研究自己,已投稿,在线蹲一个同行评议
数据的存储