【问题标题】:Render array items within groups as separate JSX elements将组内的数组项呈现为单独的 JSX 元素
【发布时间】:2020-03-03 11:08:57
【问题描述】:

我有一个数组,说 arr chapters = ["C1", "C2", "C3,...."C10", "C11"] 有 11 个章节标题。我正在尝试在 <ul> 中呈现这些作为每个 <ul> 中最多 5 个项目的组。因此,例如:

<ul>
    <li>C1</li>
    <li>C2</li>
    <li>C3</li>
    <li>C4</li>
    <li>C5</li>
</ul>
<ul>
    <li>C6</li>
    <li>C7</li>
    <li>C8</li>
    <li>C9</li>
    <li>C10</li>
</ul>
<ul>
    <li>C11</li>
</ul>

我已将数组对象传递给我的组件,但我很难弄清楚如何实现条件渲染部分。这是内部(更大的)渲染函数:

<ul>
{data.chapters && data.chapters.map((chapter,index) => (
    <li>{chapter}</li>
    //??How to conditionally add </ul><ul> here to start grouping them into 5 items
))}
</ul>

我正在考虑使用索引值来帮助我识别项目编号并有条件地呈现关闭&lt;/ul&gt;&lt;ul&gt; 标记以关闭并打开一个新的&lt;ul&gt; 标记。

我也在想,也许我应该将逻辑放在 render() 之外,并将项目预排序为 5 个数组,然后在此处渲染它们。

【问题讨论】:

    标签: javascript reactjs jsx


    【解决方案1】:

    根据索引返回标签。

    组中的第一个元素返回&lt;ul&gt;,最后一个元素返回&lt;/ul&gt;

    <ul>
    {data.chapters && data.chapters.map((chapter,index) => {
        if(index%5===1) return <ul><li>{chapter}</li>
        else if(index%5===0 || index+1===data.chapters.length) return <li>{chapter}</li></ul>
        else return <li>{chapter}</li>
    })}
    </ul>
    

    【讨论】:

    • 我试过了,但它没有将 else if 条件作为 js 代码执行。它简单地将其输出为浏览器中的文本。
    【解决方案2】:

    试试这个:

    {data.chapters && data.chapters.map((chapter,index) => {
        return (index+1) % 5 === 0 && <ul><li>{chapter}</li></ul>
    })}
    

    【讨论】:

      【解决方案3】:

      试试这个:

          const courses = ["C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10", "C11"];
          const getItems = function() {
              const lists = [];
              let listItems = [];
              for(let loopIndex = 0; loopIndex < courses.length; loopIndex++) {
                  const nextIndex = (loopIndex + 1);
                  listItems.push(<li>{ courses[loopIndex] }</li>);
                  if(((nextIndex % 5) === 0) || nextIndex === courses.length) {
                      lists.push(<ul>{ listItems }</ul>);
                      listItems = [];
                  }
              }
              return lists;
          };
      

      然后你在render 内部调用getItems

      render() {
          return (<>{ getItems() }</>);
      }
      

      【讨论】:

        【解决方案4】:
        import React from 'react';
        import './card.css';
        import _ from 'lodash';
        class Chapters extends React.Component {
            state = {
                chunk: _.chunk([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 5)
            }
        
            render() {
                return (
                    <div>
                        {this.state.chunk.map((part, index) => {
                            return <ul>
                                Group - {index}
                                {part.map((ch, i) => {
                                    return <li>{ch}</li>
                                })}
                            </ul>
                        })}
                    </div>
                )
            }
        }
        
        export default Chapters; 
        

        问题是创建&lt;ul&gt; &lt;/ul&gt;的动态迭代。正如问题中描述的上述人。

        所以我使用了 lodash chunk 函数,它将返回二维数组 const chunks = [[1,2,3,4,5], [6,7,8,9,10], [11]];

        块[0] = &lt;ul&gt; &lt;/ul&gt; 块[0] = &lt;ul&gt; &lt;/ul&gt; 块[0] = &lt;ul&gt; &lt;/ul&gt;

        【讨论】:

        • 您好 - 很好的答案 - 如果您可以添加一些解释,说明您的代码为何能与他们之前尝试做的相比,可能会更有帮助?
        • 谢谢克里斯,我已经添加了 cmets
        • @ChrisAlexander :虽然上面的答案实现了我在my answer, 中展示的完全相同的方法,但我不认为只为单个项目(可能)引入整个库使用工具箱是一个好主意,尤其是在需要的功能很容易用一行代码填充的情况下。因此,如果我不确定用户是否已使用通常由问题下的适当标记指示的库,我通常不建议包含另一个库。
        猜你喜欢
        • 1970-01-01
        • 2019-01-10
        • 1970-01-01
        • 1970-01-01
        • 2022-01-06
        • 2017-09-21
        • 2012-03-10
        • 1970-01-01
        • 2016-02-13
        相关资源
        最近更新 更多