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

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

React .map creating another container for images

问题

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

  1. // 这是容器的样式
  2. .container {
  3. display: flex;
  4. justify-content: center;
  5. align-items: center;
  6. margin: 10vmin;
  7. overflow: hidden;
  8. transform: skew(5deg);
  9. }
  10. // 这是地图
  11. <div className="grid grid-rows-3 bg-black">
  12. <div className={styles.container}>
  13. {imageList.map((url) => {
  14. return (
  15. <div className={styles.card}>
  16. <img src={url} />
  17. </div>
  18. );
  19. })}
  20. </div>
  21. </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

  1. //This is the container styles
  2. .container {
  3. display: flex;
  4. justify-content: center;
  5. align-items: center;
  6. margin: 10vmin;
  7. overflow: hidden;
  8. transform: skew(5deg);
  9. // This is the map
  10. &lt;div className=&quot; grid grid-rows-3 bg-black&quot;&gt;
  11. &lt;div className={styles.container}&gt;
  12. {imageList.map((url) =&gt; {
  13. return (
  14. &lt;div className={styles.card}&gt;
  15. &lt;img src={url} /&gt;
  16. &lt;/div&gt;
  17. );
  18. })}
  19. &lt;/div&gt;
  20. // 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.

  1. const imageListLength = imageList.length;
  2. let imageListTop, imageListBottom;
  3. // Split image list into two arrays
  4. if (imageListLength &gt; 6) {
  5. imageListTop = imageList.slice(0, 6);
  6. imageListBottom = imageList.slice(6);
  7. } else {
  8. imageListTop = imageList;
  9. imageListBottom = [];
  10. }
  11. return (
  12. &lt;div className=&quot;grid grid-rows-3 bg-black&quot;&gt;
  13. &lt;div className={styles.container}&gt;
  14. {imageListTop.map((url) =&gt; {
  15. return (
  16. &lt;div className={styles.card}&gt;
  17. &lt;img src={url} /&gt;
  18. &lt;/div&gt;
  19. );
  20. })}
  21. &lt;/div&gt;
  22. {imageListBottom.length &gt; 0 &amp;&amp; (
  23. &lt;div className={styles.container}&gt;
  24. {imageListBottom.map((url) =&gt; {
  25. return (
  26. &lt;div className={styles.card}&gt;
  27. &lt;img src={url} /&gt;
  28. &lt;/div&gt;
  29. );
  30. })}
  31. &lt;/div&gt;
  32. )}
  33. &lt;/div&gt;
  34. );

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.

  1. const imageListLength = imageList.length;
  2. let imageListTop, imageListBottom;
  3. if (imageListLength &gt; 6) {
  4. imageListTop = imageList.slice(0, 6);
  5. imageListBottom = imageList.slice(6);
  6. } else {
  7. imageListTop = imageList;
  8. imageListBottom = [];
  9. }
  10. return (
  11. &lt;div className=&quot;grid grid-rows-3 bg-black&quot;&gt;
  12. {/* Loop through the image list */}
  13. {imageList.map((url, index) =&gt; {
  14. if (index % 6 === 0) {
  15. return (
  16. &lt;div className={styles.container}&gt;
  17. {imageList.slice(index, index + 6).map((url) =&gt; {
  18. return (
  19. &lt;div className={styles.card}&gt;
  20. &lt;img src={url} /&gt;
  21. &lt;/div&gt;
  22. );
  23. })}
  24. &lt;/div&gt;
  25. );
  26. }
  27. })}
  28. &lt;/div&gt;
  29. );

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:

确定