Я хочу вызвать компонент внутри того же компонента динамически.
commentdata = [
{
"_id": "5dbc479babc1c22683b73cf3",
"comment": "wow .. this is awsome",
"children": [
{
"_id": "5dbc481ec3bb512780ebda22",
"comment": "second child",
"children": [
{
"_id": "5dbc481ec3bb512780ebda22",
"comment": "hi darling",
"children": [],
"user": {
"_id": "5dbb81c8c597533bf4c38e75",
"username": "arunkavale",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/johnsmithagency/128.jpg"
},
"updatedDate": "2019-11-01T14:58:38.188Z",
"createdDate": "2019-11-01T14:58:38.188Z"
}
],
"user": {
"_id": "5dbb81c8c597533bf4c38e75",
"username": "arunkavale",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/johnsmithagency/128.jpg"
},
"updatedDate": "2019-11-01T14:58:38.188Z",
"createdDate": "2019-11-01T14:58:38.188Z"
},
{
"_id": "5dbc481ec3bb512780ebda22",
"comment": "yep",
"children": [],
"user": {
"_id": "5dbb81c8c597533bf4c38e75",
"username": "arunkavale",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/johnsmithagency/128.jpg"
},
"updatedDate": "2019-11-01T14:58:38.188Z",
"createdDate": "2019-11-01T14:58:38.188Z"
}
],
"user": {
"_id": "5dbb9683b44bfa2a3dce55bd",
"username": "mayank",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/alxndrustinov/128.jpg"
},
"createdDate": "2019-11-01T14:56:27.580Z",
"updatedDate": "2019-11-01T14:58:38.188Z",
"__v": 0
}
]
выше - это данные списка комментариев, которые я получаю от bcakend.
import React from 'react';
import CommentDetail from './commentDetails';
class CommentList extends React.Component {
constructor(props){
super(props);
}
render(){
const comments = this.props.comments.map((comment)=>{
return <CommentDetail comment={comment.comment} key={comment._id} author = {comment.user} timeAgo={comment.createdDate}></CommentDetail>
})
return (
<div >
{comments}
</div >
)
}
}
export default CommentList;
.
import React from 'react';
import CommentAction from './commentAction';
const CommentDetail = (props) =>{
console.log(props);
return (
<div className="comment">
<a href="/" className="avatar">
<img alt="avatar" src={props.author.avatar} />
</a>
<div className="content">
<a href="/" className="author">
{props.author.username}
</a>
<div className="metadata">
<span className="date">
{props.timeAgo}
</span>
</div>
<div className="text">{props.comment}</div>
<CommentAction user={props.author}></CommentAction>
</div>
</div>
)
}
export default CommentDetail;
вышеупомянутый код работает нормально, но я хочу что-то вроде
import React from 'react';
import CommentDetail from './commentDetails';
class CommentList extends React.Component {
constructor(props){
super(props);
}
render(){
const comments = this.props.comments.map((comment)=>{
if(comment.children.length>0){
let children=[];
for (let i = 0; i < comment.children.length; i++) {
let commentComp = <CommentDetail comment={comment.comment} key={comment._id} author = {comment.user} timeAgo={comment.createdDate}>
<CommentDetail comment={comment.children[i].comment} key={comment.children[i]._id} author = {comment.children[i].user} timeAgo={comment.children[i].createdDate}>
</CommentDetail>
</CommentDetail>
children.push(commentComp)
}
return children
}
return <CommentDetail comment={comment.comment} key={comment._id} author = {comment.user} timeAgo={comment.createdDate}></CommentDetail>
})
return (
<div >
{comments}
</div >
)
}
}
export default CommentList;
здесь функция должна проверить, есть ли дети, если да, тогда она должна вызвать CommentDetail с вложенными всеми его детьми какв компоненте CommentDetail. Я попробовал рекурсивную функцию, но я не получил ... пожалуйста, помогите мне с этим. и заранее спасибо.