当前位置:网站首页>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;
四、效果展示
今天的分享到此结束,欢迎小伙伴们一起交流
边栏推荐
- GPT-3当一作自己研究自己,已投稿,在线蹲一个同行评议
- Mobile measurement and depth link platform - Branch
- ubuntu20安装redisjson记录
- Codeworks 5 questions per day (1700 average) - day 7
- Gpt-3 is a peer review online when it has been submitted for its own research
- termux设置电脑连接手机。(敲打命令贼快),手机termux端口8022
- QT opens a file and uses QFileDialog to obtain the file name, content, etc
- 太方便了,钉钉上就可完成代码发布审批啦!
- 力扣------路径总和 III
- [safe office and productivity application] Shanghai daoning provides you with onlyoffice download, trial and tutorial
猜你喜欢
2022年上半年HIT行业TOP50
Set static IP for raspberry pie
25. (ArcGIS API for JS) ArcGIS API for JS line modification line editing (sketchviewmodel)
Arduino droplet detection
代码质量管理
Not All Points Are Equal Learning Highly Efficient Point-based Detectors for 3D LiDAR Point
什么是 BA ?BA怎么样?BA和BI是什么关系?
About Confidence Intervals
维护万星开源向量数据库是什么体验
VHDL implementation of arbitrary size matrix addition operation
随机推荐
[security attack and Defense] how much do you know about serialization and deserialization?
小程序能运行在自有App中,且实现直播和连麦?
QT 使用QToolTip 鼠标放上去显示文字时会把按钮的图片也显示了、修改提示文字样式
力扣------路径总和 III
本机mysql
【DPDK】dpdk样例源码解析之三:dpdk-l3fwd_001
2022年上半年HIT行业TOP50
[leetcode] 700 and 701 (search and insert of binary search tree)
数据的存储
MySQL storage engine
[MySQL] row sorting in MySQL
About Confidence Intervals
Native MySQL
OSCP工具之一: dirsearch用法大全
Codeworks 5 questions per day (1700 average) - day 7
Vernacular high concurrency (2)
Adaptive non European advertising retrieval system amcad
My brave way to line -- elaborate on what happens when the browser enters the URL
海思3559万能平台搭建:RTSP实时播放的支持
SQL injection -day15