[英]How to list a data inside another list using map and filter in React CLI (Comments List and Reply List)
I have a Simple comment List, Where I can list all comments, but I need is list its child (reply) list inside it by matching the commentId.我有一个简单的评论列表,我可以在其中列出所有评论,但我需要通过匹配 commentId 在其中列出它的子(回复)列表。 Here is the code am working with.这是我正在使用的代码。
class comments extends Component {
constructor(props) {
super(props);
this.state = {
blogList: {},
commentList: [{"id":"1","post_id":"1","username":"Rovan","content":"Wow","avatar":"","created_on":"2020-01-16","updated_on":null},{"id":"2","post_id":"1","username":"Ravan","content":"Woooow!","avatar":"","created_on":"2020-01-16","updated_on":null},{"id":"4","post_id":"1","username":"Ravan V","content":"Yes Ram","avatar":"","created_on":"2020-01-19","updated_on":null},{"id":"5","post_id":"1","username":"Ravan V","content":"Yes Ram","avatar":"","created_on":"2020-01-19","updated_on":null}],
ReplyList: [{"id":"1","comment_id":"1","post_id":"1","username":"Sam","content":"wow","avatar":"","created_on":"2020-01-15","updtaed_on":null},{"id":"2","comment_id":"1","post_id":"1","username":"Ron","content":"Yes! Yes!","avatar":"","created_on":"2020-01-14","updtaed_on":null},{"id":"3","comment_id":"2","post_id":"1","username":"Sam","content":"wow","avatar":"","created_on":"2020-01-15","updtaed_on":null}]
};
}
render() {
const { commentList } = this.state;
const { ReplyList } = this.state;
const replyIterationMap = (id)=>{
ReplyList.filter(c => c.comment_id === id).map((item, index) =>(
<div className="media d-block d-md-flex mt-3" key={index}>
<div
className={
Style.avatar +
" " +
this.AvatarColorChange(item.username.substring(0, 1))
}
>
<h2 className="text-center pt-1 mt-md-1 mt-sm-2 mt-lg-1">
{item.username.substring(0, 1).toUpperCase()}
</h2>
</div>
<div className="media-body text-center text-md-left ml-md-3 ml-0">
<h5 className="mt-0 font-weight-bold">
{item.username}
<span
className={
Style.relativeDate + " float-right font-weight-lighter"
}
>
<Moment format="MMM 'DD">{item.created_on}</Moment>
</span>
</h5>
<p>{item.content}</p>
<hr />
</div>
</div>
))};
const commentIterationMap = commentList.map((item, index) => (
<div
className={Style.comments + " media d-block d-md-flex mt-4"}
key={index.toString()}
>
<div
className={
Style.avatar +
" " +
this.AvatarColorChange(item.username.substring(0, 1))
}
>
<h2 className="text-center pt-1 mt-md-1 mt-sm-2 mt-lg-1">
{item.username.substring(0, 1).toUpperCase()}
</h2>
</div>
<div className="media-body text-center text-md-left ml-md-3 ml-0">
<h5 className="font-weight-bold">
{item.username}
<span
className={
Style.relativeDate + " float-right font-weight-lighter"
}
>
<Moment format="MMM DD">{item.created_on}</Moment>
</span>
</h5>
<p>{item.content} </p>
<div className="text-right pt-1">
<a href="#i" id="reply">
Replay
</a>
</div>
<hr />
{ replyIterationMap(item.id)}
</div>
</div>
));
return (
<div className="comments">
{commentIterationMap}
</div>
);}
}
export default comments;
I want to get list all comments and corresponding reply by macting the comments.我想通过 macting 评论来列出所有评论和相应的回复。 Like below image像下图
I tried the above code it didn't work for me, i need to list those comments and reply above image我尝试了上面的代码它对我不起作用,我需要列出这些评论并回复上面的图片
In the render
method, you are not calling the commentIterationMap
method.在render
方法中,您没有调用commentIterationMap
方法。 Also, in the replyIterationMap
you are not returning the JSX for the replies.此外,在replyIterationMap
您不会为回复返回 JSX。
Furthermore, I would declare the commentIterationMap
and replyIterationMap
methods as methods of the class (instead of defining them in the render method).此外,我将commentIterationMap
和replyIterationMap
方法声明为类的方法(而不是在render 方法中定义它们)。 I would also call them differently ( renderComments
and renderReplies
respectively), as to note that they are responsible for rendering some parts of the JSX.我也会以不同的方式调用它们(分别是renderComments
和renderReplies
),因为它们负责渲染 JSX 的某些部分。
Taking all that into account, the code would look like:考虑到所有这些,代码如下所示:
class comments extends Component {
constructor(props) {
super(props);
this.state = {
blogList: {},
commentList: [{
"id":"1",
"post_id":"1",
"username":"Rovan","content":"Wow","avatar":"","created_on":"2020-01-16","updated_on":null
},{"id":"2","post_id":"1","username":"Ravan","content":"Woooow!","avatar":"","created_on":"2020-01-16","updated_on":null},{"id":"4","post_id":"1","username":"Ravan V","content":"Yes Ram","avatar":"","created_on":"2020-01-19","updated_on":null},{"id":"5","post_id":"1","username":"Ravan V","content":"Yes Ram","avatar":"","created_on":"2020-01-19","updated_on":null}],
ReplyList: [{"id":"1","comment_id":"1","post_id":"1","username":"Sam","content":"wow","avatar":"","created_on":"2020-01-15","updtaed_on":null},{"id":"2","comment_id":"1","post_id":"1","username":"Ron","content":"Yes! Yes!","avatar":"","created_on":"2020-01-14","updtaed_on":null},{"id":"3","comment_id":"2","post_id":"1","username":"Sam","content":"wow","avatar":"","created_on":"2020-01-15","updtaed_on":null}]
};
}
renderReplies(commentId) {
return this.state.ReplyList
.filter(c => c.comment_id === commentId)
.map((item, index) => (
<div className="media d-block d-md-flex mt-3" key={index}>
<div
className={
Style.avatar +
" " +
this.AvatarColorChange(item.username.substring(0, 1))
}
>
<h2 className="text-center pt-1 mt-md-1 mt-sm-2 mt-lg-1">
{item.username.substring(0, 1).toUpperCase()}
</h2>
</div>
<div className="media-body text-center text-md-left ml-md-3 ml-0">
<h5 className="mt-0 font-weight-bold">
{item.username}
<span
className={
Style.relativeDate + " float-right font-weight-lighter"
}
>
<Moment format="MMM 'DD">{item.created_on}</Moment>
</span>
</h5>
<p>{item.content}</p>
<hr />
</div>
</div>
));
}
renderComments() {
return this.state.commentList.map((item, index) => (
<div
className={Style.comments + " media d-block d-md-flex mt-4"}
key={index.toString()}
>
<div
className={
Style.avatar +
" " +
this.AvatarColorChange(item.username.substring(0, 1))
}
>
<h2 className="text-center pt-1 mt-md-1 mt-sm-2 mt-lg-1">
{item.username.substring(0, 1).toUpperCase()}
</h2>
</div>
<div className="media-body text-center text-md-left ml-md-3 ml-0">
<h5 className="font-weight-bold">
{item.username}
<span
className={
Style.relativeDate + " float-right font-weight-lighter"
}
>
<Moment format="MMM DD">{item.created_on}</Moment>
</span>
</h5>
<p>{item.content} </p>
<div className="text-right pt-1">
<a href="#i" id="reply">
Replay
</a>
</div>
<hr />
{this.replyIterationMap(item.id)}
</div>
</div>
));
}
render() {
return (
<div className="comments">
{this.commentIterationMap()}
</div>
);
}
}
export default comments;
This method too works on it.这种方法也适用于它。
class Comment extends React.Component {
constructor(props) {
super(props)
this.state = {
commentList: [{"id":"1","post_id":"1","username":"Rovan","content":"Wow","avatar":"","created_on":"2020-01-16","updated_on":null},{"id":"2","post_id":"1","username":"Ravan","content":"Woooow!","avatar":"","created_on":"2020-01-16","updated_on":null},{"id":"4","post_id":"1","username":"Ravan V","content":"Yes Ram","avatar":"","created_on":"2020-01-19","updated_on":null},{"id":"5","post_id":"1","username":"Ravan V","content":"Yes Ram","avatar":"","created_on":"2020-01-19","updated_on":null}],
ReplyList: [{"id":"1","comment_id":"1","post_id":"1","username":"Sam","content":"wow","avatar":"","created_on":"2020-01-15","updtaed_on":null},{"id":"2","comment_id":"1","post_id":"1","username":"Ron","content":"Yes! Yes!","avatar":"","created_on":"2020-01-14","updtaed_on":null},{"id":"3","comment_id":"2","post_id":"1","username":"Sam","content":"wow","avatar":"","created_on":"2020-01-15","updtaed_on":null}]
}
}
render() {
const { commentList } = this.state;
const { ReplyList } = this.state;
const replyIterationMap = (id)=>{
//console.log(id);
return ReplyList.filter(c => c.comment_id === id).map((item, index) => (
<div className="media d-block d-md-flex mt-3" key={index}>
<div
className="avatar"
>
<h2 className="text-center pt-1 mt-md-1 mt-sm-2 mt-lg-1">
{item.username.substring(0, 1).toUpperCase()}
</h2>
</div>
<div className="media-body text-center text-md-left ml-md-3 ml-0">
<h5 className="mt-0 font-weight-bold">
{item.username}
<span
className={" float-right font-weight-lighter"
}
>
{item.created_on}
</span>
</h5>
<p>{item.content}</p>
<hr />
</div>
</div>
))};
const commentIterationMap = commentList.map((item, index) => (
<div
className=" media d-block d-md-flex mt-4"
key={index.toString()}
>
<div
className="avatar"
>
<h2 className="text-center pt-1 mt-md-1 mt-sm-2 mt-lg-1">
{item.username.substring(0, 1).toUpperCase()}
</h2>
</div>
<div className="media-body text-center text-md-left ml-md-3 ml-0">
<h5 className="font-weight-bold">
{item.username}
<span
className="float-right font-weight-lighter"
>
{item.created_on}
</span>
</h5>
<p>{item.content} </p>
<div className="text-right pt-1">
<a href="#i" id="reply">
Replay
</a>
</div>
<hr />
{ replyIterationMap(item.id)}
</div>
</div>
));
return (
<div className="row">
<div className="col-md-12">
<div className="comments">
<div className="card mb-3 wow fadeIn">
<div className="card-header font-weight-bold">
{commentList.length} Comments
</div>
<div className="card-body">{commentIterationMap}</div>
</div>
</div>
</div>
</div>
)
}
}
export default Comment;
Here is the working Live Code demo Link Jsfiddle这是有效的实时代码演示链接Jsfiddle
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.