简体   繁体   English

JSX 中断......使用映射生成条件 JSX 元素循环遍历数组

[英]JSX breaks ... Looping over an array with map generating conditional JSX elements

I am having issues trying to build a custom component with a looping array that conditionally renders different elements.我在尝试使用循环数组构建自定义组件时遇到问题,该数组有条件地呈现不同的元素。 That component contains special classNames for generating a grid of 3 elements per row, independently of the number of parameters passed to the component.该组件包含特殊的类名,用于生成每行 3 个元素的网格,与传递给组件的参数数量无关。

I am using map for looping through a conditional index where I render conditionally the first element (for opening), normal elements, every 3 elements (for closing column and opening again), and the final element.我正在使用map循环遍历条件索引,其中我有条件地渲染第一个元素(用于打开)、普通元素、每 3 个元素(用于关闭列并再次打开)和最后一个元素。

    let optionsLength = this.props.options.length;
    let options = []

    this.props.options.map((option, index) => {
          if (index === 0) {
            options.push(
          <div className="column is-one-third">
          ...
          // other conditionals explained below

The problem comes trying to find a way where every 3 elements I need to close and open the parent <div> .问题是试图找到一种方法,我需要关闭和打开父<div>每 3 个元素。

I would like to close the </div> tag and open a new column div <div className="columns sub"> before just appending the element.我想关闭</div>标签并在追加元素之前打开一个新列 div <div className="columns sub"> That iteration cannot be closed in that same conditional as the styles would break an there is more logic that depends on the following elements.该迭代不能在相同的条件下关闭,因为样式会破坏更多的逻辑取决于以下元素。 But on the other side, JSX breaks.但另一方面,JSX 中断了。

Here is some pseudocode of what I would love to achieve:这是我想要实现的一些伪代码:

<div classname="columns"> // <-- ideally, rendered with the same element
  <div>...</div> //<-- 1st element
  <div>...</div>// <-- 2nd element
  <div>...</div>// <-- 3rd element, so we add the following 2 lines
</div> <-- 3rd element
<div className="columns"> //<-- 3rd element
<div>...</div> //<-- 4rd element
...
<div>....</div>//<-- last element
</div>// <-- last element

PS I am using bulma for generating the columns grid. PS 我正在使用 bulma 来生成列网格。

Assuming this.props.options is [1,2,3,4,5,6,7] , I declare this method for the component:假设this.props.options[1,2,3,4,5,6,7] ,我为组件声明这个方法:

    treatArray = arr => {
      let masterArray = []
      // Will a new array of arrays with 3 items
      while (arr.length) masterArray.push(arr.splice(0, 3));

      return (
        <div>
          {masterArray.map(setOf3 => (
            <div classname="columns">
              {setOf3.map(singleCol => (
                <div className="column is-one-third">{singleCol}</div>
              ))}
            </div>
          ))}
        </div>
      );
    };

And then invoke this function in your render :然后在你的render调用这个函数:

render(){
    return(
     //.
     //.
        { this.treatArray(this.props.options) } 
     //.
     //.
    )
}

Here is a working codesandbox .这是一个有效的代码和框

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM