当前位置:网站首页>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
边栏推荐
- Create commonly used shortcut icons at the top of the ad interface (menu bar)
- HW notes (II)
- 2022中青杯数学建模B题开放三孩背景下的生育政策研究思路
- QT 项目 表格新建列名称设置 需求练习(找数组消失的数字、最大值)
- easyui出口excel无法下载框弹出的办法来解决
- 接口数据安全保证的10种方式
- POJ培训计划2253_Frogger(最短/floyd)
- . Net interface can be implemented by default
- A 股指数成分数据 API 数据接口
- Probability formula
猜你喜欢
[security attack and Defense] how much do you know about serialization and deserialization?
Kotlin Android 环境搭建
2022中青杯C题城市交通思路分析
ABAP 動態內錶分組循環
Probability formula
什么是 BA ?BA怎么样?BA和BI是什么关系?
Kbone与小程序跨端开发的一些思考
[leetcode] 450 and 98 (deletion and verification of binary search tree)
机器学习笔记 - 使用机器学习进行鸟类物种分类
Quick completion guide of manipulator (10): accessible workspace
随机推荐
Antd Comment 递归循环评论
概率论公式
What is Ba? How about Ba? What is the relationship between Ba and Bi?
VHDL implementation of arbitrary size matrix multiplication
什么是 CGI,什么是 IIS,什么是VPS「建议收藏」
[leetcode] 700 and 701 (search and insert of binary search tree)
Do you choose pandas or SQL for the top 1 of data analysis in your mind?
Summer 2022 daily question 1 (1)
Arduino droplet detection
vim —- 自己主动的按钮indent该命令「建议收藏」
It's too convenient. You can complete the code release and approval by nailing it!
Termux set up the computer to connect to the mobile phone. (knock the command quickly), mobile phone termux port 8022
web服务性能监控方案
一些常用软件相关
golang 压缩和解压zip文件
Storage of data
Class常量池与运行时常量池
Hisilicon 3559 universal platform construction: RTSP real-time playback support
Mysql-数据丢失,分析binlog日志文件
杭州电 3711 Binary Number