【发布时间】:2019-11-26 11:11:46
【问题描述】:
我目前在我的 IndexPage 组件中使用 Gatsby 和 gatsby-image。我希望将以下代码重构为单个 React 组件,因为存在不必要的重复:
...
return (
<div>
<h1>Startups</h1>
<p>Copy</p>
<Img fluid={props.data.imageOne.childImageSharp.fluid}/>
</div>
<div>
<h1>People</h1>
<p>Copy</p>
<Img fluid={props.data.imageTwo.childImageSharp.fluid}/>
</div>
<div>
<h1>Data</h1>
<p>Copy</p>
<Img fluid={props.data.imageThree.childImageSharp.fluid}/>
</div>
</div>
)
export const fluidImage = graphql`
fragment fluidImage on File {
childImageSharp {
fluid(maxWidth: 1000) {
...GatsbyImageSharpFluid
}
}
}
`
export const pageQuery = graphql`
query {
imageOne: file(relativePath: { eq: "igemA.png" }) {
...fluidImage
}
imageTwo: file(relativePath: { eq: "inephemeraA.png" }) {
...fluidImage
}
imageThree: file(relativePath: { eq: "polypA.png" }) {
...fluidImage
}
}
类似于这样的:
const NewComponent = (props) => (
<div>
<h1>props.heading</h1>
<p>props.body</p>
<Img fluid={props.data.[props.image].childImageSharp.fluid}/>
</div>
)
如何更改 graphql 查询,以便我可以根据传递给 NewComponent 的道具渲染图像?
【问题讨论】:
-
与您现在修改查询的方式相同吗? developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…