【发布时间】:2021-01-02 20:40:50
【问题描述】:
所以我有渲染 2 个不同图像的图像组件,但我希望能够将我的图像组件添加到任何页面,并将特定图像作为道具传递,而不必硬编码新的查询我需要使用的每张图片。
如果我有 50 张图片,我只想将 image-50.jpg 作为道具传入,而不是对其进行特定查询。有没有办法在 gatsby 中使用 graphql 来做到这一点?
这是我目前的图片组件代码
import { graphql, useStaticQuery } from "gatsby"
import Img from "gatsby-image"
import React from "react"
const Image = () => {
const data = useStaticQuery(graphql`
query {
astronaut: file(relativePath: { eq: "gatsby-astronaut.png" }) {
childImageSharp {
fluid(maxHeight: 600) {
...GatsbyImageSharpFluid
}
}
}
person: file(relativePath: { eq: "profile.jpg" }) {
childImageSharp {
fluid(maxHeight: 600) {
...GatsbyImageSharpFluid
}
}
}
}
`)
return (
<>
<Img fluid={data.astronaut.childImageSharp.fluid} />
<Img fluid={data.person.childImageSharp.fluid} />
</>
)
}
export default Image
有没有办法只动态添加图像,然后将其渲染到新页面?
类似 <Image src={"profile.jpg"} />
我不知道如何将它添加到 graphql 并想象我有 50 张图片,然后我必须映射所有 50 张图片或手动添加每个查询,这没有意义
【问题讨论】:
标签: reactjs gatsby gatsby-image