首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >gatsby-image映射多个查询

gatsby-image映射多个查询
EN

Stack Overflow用户
提问于 2019-11-04 13:37:44
回答 1查看 499关注 0票数 0

我有下面的组件。对于图像,我想使用gatsby-image和艺术指导多个图像。gatsby-image中的示例可以处理单个图像,但是我如何将其应用于包含多个图像的组件?

子组件:

代码语言:javascript
复制
const Cards = ({ className, items }) => {
  return (
    <section className={className}>
      <div className="grid">
        {items.map(item => {
          return (
            <div className="card">
              <img src={item.image} alt="" />
              <h3>{item.title}</h3>
              <p>{item.desc}</p>
            </div>
          )
        })}
      </div>
    </section>
  )
}

export default Cards

页面组件:

代码语言:javascript
复制
const IndexPage = () => (
  <Layout>
    <Cards items={cards} />
  </Layout>
)

const cards = [
  {
    title: 'Card One',
    desc: 'Description',
    image: require('../images/image.jpg'),
  },
  {
    title: 'Card Two',
    desc: 'Description',
    image: require('../images/image2.jpg'),
  },
  {
    title: 'Card Three',
    desc: 'Description',
    image: require('../images/image3.jpg'),
  },
]

export default IndexPage

来自gatsby-image文档的示例:

代码语言:javascript
复制
import React from "react"
import { graphql } from "gatsby"
import Img from "gatsby-image"

export default ({ data }) => {
  // Set up the array of image data and `media` keys.
  // You can have as many entries as you'd like.
  const sources = [
    data.mobileImage.childImageSharp.fluid,
    {
      ...data.desktopImage.childImageSharp.fluid,
      media: `(min-width: 768px)`,
    },
  ]

  return (
    <div>
      <h1>Hello art-directed gatsby-image</h1>
      <Img fluid={sources} />
    </div>
  )
}

export const query = graphql`
  query {
    mobileImage: file(relativePath: { eq: "blog/avatars/kyle-mathews.jpeg" }) {
      childImageSharp {
        fluid(maxWidth: 1000, quality: 100) {
          ...GatsbyImageSharpFluid
        }
      }
    }
    desktopImage: file(
      relativePath: { eq: "blog/avatars/kyle-mathews-desktop.jpeg" }
    ) {
      childImageSharp {
        fluid(maxWidth: 2000, quality: 100) {
          ...GatsbyImageSharpFluid
        }
      }
    }
  }
`

EN

回答 1

Stack Overflow用户

发布于 2020-02-10 15:03:35

您需要使用gatsby-transformer-remark并编写markdown。

这是official tutorial。此外,gatsby-starter-blog使用gatsby-transformer-remark,因此值得一看。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58687728

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档