反应嵌入式循环

问题描述 投票:-1回答:1

我有一个包含很少元素的对象列表,单个元素看起来像:

{
    'id': '5d4647dsa54d6s4ad65as',
    'date': '2016-06-09T15:03:12.000Z',
    'title': 'Istvan 5',
    'text': 'Pathos, betrayal, "The Galaxy will be Burning!", ',
    'coments': [
        {
            'id': 'qwerty',
            'user': 'Gu1iman',
            'text': 'The Code of Adeptus Astartes does not approve of     this'
        },
        {
            'id': 'asdfg',
            'user': 'Feru5 Manu5',
            'text': 'I lost my had for love!'
        },
        {
            'id': 'zxcvb',
            'user': 'Fu1grim Fenix',
            'text': 'I know bro, I know'
        },
    ]
},

我可以显示文字:

export default class ArticleList extends PureComponent
  {
   state = {
       openArticleId: null
   }
   render(){
       const articleElements = this.props.articles.map((articles,    index)     =>
           <li key={articles.id} className='article-list__li' onClick=              
  {this.handelClick.bind(this, articles.id)}>
               <Article article = {articles}
                        isOpen = {this.state.openArticleId ===    articles.id}
                        onButtonClick = {this.handelClick.bind(this, articles.id)}/>
           </li>
       )
       return (
           <ul>
               {articleElements}
           </ul>
       )
   }
   handelClick = openArticleId => this.setState({
       openArticleId: this.state.openArticleId === openArticleId ?    null : openArticleId
   })
}

但我不介意如何显示本文的“评论”。我该如何显示?我曾尝试编写嵌入式循环,但它无法正常工作。

javascript reactjs
1个回答
0
投票

<Article article = {articles} - 使用您已经传递的数据:

this.props.article.comments.map(

当然,内部呈现<Article />。结果将在<li />标记内呈现。当然,您只能使用isOpen prop为'open'文章呈现评论:

(!!this.props.isOpen && this.props.article.comments.map(
© www.soinside.com 2019 - 2024. All rights reserved.