我正在使用 gatsby.js 构建一个投资组合网站。所有照片都发布在 WordPress 中,由 graphQL 获取并渲染到网站。
我正在尝试使用 bootstrap grid 来组织照片并使其响应,但是因为 graphQL 返回一个包含从 WordPress 帖子中获取的所有图像的数组,所以我无法像我一样设置带有 class='row' 的 div m 使用 array.map。而且我不知道如何解决。
在 graphQL 中,我将分辨率设置为 width=300px 和 height=300px。
这是我发现组织尺寸的唯一方法,只要我不能使用类行并且所有图像都被考虑在一行中。问题是照片尺寸不响应,所以永远是300X300...
我想要一种方法使其成为网格系统,因为它应该可以工作......所以当我调整窗口大小时,所有照片都会被组织和调整大小。
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
数组拆分为分块数组,则可以循环遍历外部数组来渲染行
,以及每个要渲染 cols 的内部数组。
对于 3 列引导网格,您需要传入大小值 3(在此示例中,这是 lodash.chunk
的第二个参数)。这确保了每个 block 的长度为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'));
关于wordpress - 如何使用引导网格映射图像数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56149440/