React中的`.map`创建另一个用于图像的容器。

huangapple go评论59阅读模式
英文:

React .map creating another container for images

问题

我有一个使用display: flex的地图,其中包含一些图片,以及一个Firebase存储,我可以上传图片,上传的图片会显示在该flex画廊中,但该画廊中不应该超过6张图片。当上传超过6张图片时,应该创建另一个容器,新图片放在其中。这些图片被保存在状态中,以下是代码:

// 这是容器的样式
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 10vmin;
  overflow: hidden;
  transform: skew(5deg);
}

// 这是地图
<div className="grid grid-rows-3 bg-black">
  <div className={styles.container}>
    {imageList.map((url) => {
      return (
        <div className={styles.card}>
          <img src={url} />
        </div>
      );
    })}
  </div>
</div>

我需要让这个地图在一个容器中有6张图片后,自动创建下一行的新容器。

谢谢你的帮助。

英文:

I have a map with display flex with some images and a firebase storage where I can upload a pic, and the pic gets shown in that flex gallery, but there should not be more than 6 in that gallery, and when more than 6 pictures are uploaded, another container should be made where the new pics get placed, the images are held in a state,the code is here

//This is the container styles
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 10vmin;
  overflow: hidden;
  transform: skew(5deg);

// This is the map
&lt;div className=&quot; grid grid-rows-3 bg-black&quot;&gt;
        &lt;div className={styles.container}&gt;
          {imageList.map((url) =&gt; {
            return (
              &lt;div className={styles.card}&gt;
                &lt;img src={url} /&gt;
              &lt;/div&gt;
            );
          })}
        &lt;/div&gt;


// This is the


I need to make that map somehow know that once a container gets to 6 pictures, it should make another one in the row below,

Thank you for helping

答案1

得分: 1

以下是您要的翻译部分:

"You could try using the Array.prototype.slice() method to separate the image list into two arrays and then render them with two separate maps.

const imageListLength = imageList.length;
let imageListTop, imageListBottom;

// Split image list into two arrays
if (imageListLength > 6) {
imageListTop = imageList.slice(0, 6);
imageListBottom = imageList.slice(6);
} else {
imageListTop = imageList;
imageListBottom = [];
}

return (

{imageListTop.map((url) => {
return (

React中的`.map`创建另一个用于图像的容器。

);
})}

{imageListBottom.length > 0 && (

{imageListBottom.map((url) => {
return (

React中的`.map`创建另一个用于图像的容器。

);
})}

)}

);"

EDIT Taking your answer:

"您可以尝试使用Array.prototype.slice()方法将图像列表分成两个数组,然后使用两个单独的映射来呈现它们。

const imageListLength = imageList.length;
let imageListTop, imageListBottom;

if (imageListLength > 6) {
imageListTop = imageList.slice(0, 6);
imageListBottom = imageList.slice(6);
} else {
imageListTop = imageList;
imageListBottom = [];
}

return (

{/* 循环遍历图像列表 */}
{imageList.map((url, index) => {
if (index % 6 === 0) {
return (

{imageList.slice(index, index + 6).map((url) => {
return (

React中的`.map`创建另一个用于图像的容器。

);
})}

);
}
})}

);"

英文:

You could try using the Array.prototype.slice() method to separate the image list into two arrays and then render them with two separate maps.

const imageListLength = imageList.length;
let imageListTop, imageListBottom;

// Split image list into two arrays
if (imageListLength &gt; 6) {
  imageListTop = imageList.slice(0, 6);
  imageListBottom = imageList.slice(6);
} else {
  imageListTop = imageList;
  imageListBottom = [];
}

return (
  &lt;div className=&quot;grid grid-rows-3 bg-black&quot;&gt;
    &lt;div className={styles.container}&gt;
      {imageListTop.map((url) =&gt; {
        return (
          &lt;div className={styles.card}&gt;
            &lt;img src={url} /&gt;
          &lt;/div&gt;
        );
      })}
    &lt;/div&gt;
    {imageListBottom.length &gt; 0 &amp;&amp; (
      &lt;div className={styles.container}&gt;
        {imageListBottom.map((url) =&gt; {
          return (
            &lt;div className={styles.card}&gt;
              &lt;img src={url} /&gt;
            &lt;/div&gt;
          );
        })}
      &lt;/div&gt;
    )}
  &lt;/div&gt;
);

EDIT Taking your answer:

You could expand on your code by adding a loop to the code that renders the images. This loop would check the length of the image list and render the images in groups of 6 until the list is exhausted.

const imageListLength = imageList.length;
let imageListTop, imageListBottom;

if (imageListLength &gt; 6) {
  imageListTop = imageList.slice(0, 6);
  imageListBottom = imageList.slice(6);
} else {
  imageListTop = imageList;
  imageListBottom = [];
}

return (
  &lt;div className=&quot;grid grid-rows-3 bg-black&quot;&gt;
    {/* Loop through the image list */}
    {imageList.map((url, index) =&gt; {
      if (index % 6 === 0) {
        return (
          &lt;div className={styles.container}&gt;
            {imageList.slice(index, index + 6).map((url) =&gt; {
              return (
                &lt;div className={styles.card}&gt;
                  &lt;img src={url} /&gt;
                &lt;/div&gt;
              );
            })}
          &lt;/div&gt;
        );
      }
    })}
  &lt;/div&gt;
);

huangapple
  • 本文由 发表于 2023年2月18日 18:33:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定