当前位置:网站首页>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;
四、效果展示
今天的分享到此结束,欢迎小伙伴们一起交流
边栏推荐
猜你喜欢

我的勇敢对线之路--详细阐述,浏览器输入URL发生了什么

Ubuntu20 installation redisjson record

leetcode:面试题 17.24. 子矩阵最大累加和(待研究)

Graphical tools package yolov5 and generate executable files exe

On file uploading of network security

Ubuntu 20 installation des enregistrements redisjson

复杂因子计算优化案例:深度不平衡、买卖压力指标、波动率计算

24. (ArcGIS API for JS) ArcGIS API for JS point modification point editing (sketchviewmodel)

About Tolerance Intervals

Open3D 网格滤波
随机推荐
【开发软件】 tilipa开发者软件
Set static IP for raspberry pie
二进制、八进制、十六进制
Baidu map JS development, open a blank, bmapgl is not defined, err_ FILE_ NOT_ FOUND
U.S. Air Force Research Laboratory, "exploring the vulnerability and robustness of deep learning systems", the latest 85 page technical report in 2022
Lab1 configuration script
Ubuntu 20 installation des enregistrements redisjson
Principle of attention mechanism
卡尔曼滤波-1
海思3559万能平台搭建:RTSP实时播放的支持
24. (ArcGIS API for JS) ArcGIS API for JS point modification point editing (sketchviewmodel)
数据的存储
再AD 的 界面顶部(菜单栏)创建常用的快捷图标
海思万能平台搭建:颜色空间转换YUV2RGB
What is the experience of maintaining Wanxing open source vector database
运算放大器应用汇总1
Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
About Confidence Intervals
Enumeration general interface & enumeration usage specification
Tencent cloud native database tdsql-c was selected into the cloud native product catalog of the Academy of communications and communications