如何使用 bootstrap 网格映射图像数组?

How can I use bootstrap grid mapping an array of images?

我正在使用 gatsby.js 构建一个投资组合网站。所有照片都发布在 wordpress 中,由 graphQL 提取并呈现到网站上。

我正在尝试使用 bootstrap 网格来组织照片并使其响应,但是因为 graphQL return 一个包含从 wordpress 帖子中获取的所有图像的数组,我无法设置div 与 class='row' 因为我正在使用 array.map。而且我不知道怎么解决。

我在 graphQL 中将分辨率设置为宽度 = 300 像素和高度 = 300 像素。

这是我发现的唯一组织尺寸的方法,只要我不能使用 class 行并且所有图像都被视为一行。问题是照片尺寸没反应,所以一直是300X300...

我想要一种使它成为网格系统的方法,因为它应该可以正常工作...因此,当我调整 window 大小时,所有照片都会进行整理和调整大小。


const IndexPage = () => {
    const data = useStaticQuery(graphql`
        query {
            allWordpressPost {
                edges {
                    node {
                        title
                        featured_media {
                            localFile {
                                childImageSharp {
                                    resolutions(width: 300, height: 300) {
                                        src
                                        width
                                        height
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    `);
    const imagesResolutions = data.allWordpressPost.edges.map(
        (edge) => edge.node.featured_media.localFile.childImageSharp.resolutions
    );
    return (
        <Layout>
            <Jumbotron />
            <div className="container">
                <h1 className="my-5 text-center">Portfolio</h1>
                {imagesResolutions.map((imageRes) => (
                    <Img className="col-sm-6 col-lg-4 img-rounded img" resolutions={imageRes} key={imageRes.src} />
                ))}
            </div>
        </Layout>
    );
};

如果将 data.allWordpressPost.edges 数组拆分为 分块数组 ,则可以循环遍历外部数组以呈现 rows,并且每个内部数组要渲染的数组 cols

对于 3 列 bootstrap 网格,您希望传入大小值 3(在本例中是 lodash.chunk 的第二个参数)。这确保每个块的长度为 3.

这是一个简单的例子,忽略了 graphql、childImageSharp 和 gatsby-image 的使用。

import ReactDOM from 'react-dom';
import React, { Component } from 'react';
import arrayChunk from 'lodash.chunk';
import 'bootstrap/dist/css/bootstrap.min.css';

const IndexPage = () => {

  const rawData = [1, 2, 3, 4, 5, 6];
  const chunkedData = arrayChunk(rawData, 3)

  return (
    <div>
      <div className="container">
        {chunkedData.map((row, rowIndex) => {
          return (<div key={rowIndex} className="row">{
            row.map((col, colIndex) => {return (<div key={colIndex} className="col-sm">{col}</div>)})
          }</div>)
        }
        )}
      </div>
    </div>
  );
};

ReactDOM.render(<IndexPage />, document.getElementById('root'));

stackblitz