4

I am trying for the first time GatsbyJs and I see the feature with ImageSharp for optimizing images.

Now, I created a component and it works fine, but I would like to optimize the code to be able to add more images.

The code is organized in this manner: I have the component images.js, import this component in the index page. In the component at this moment have 2 images (1.jpg, 2.jpg)

I would like to create two returns of a single component image, see my code of images.js component below:

import React from "react"
import { useStaticQuery, graphql } from "gatsby"
import Image from "gatsby-image"

const ImagesHome = () => {
  const data = useStaticQuery(graphql`
    query BioQuery {
      imgOne: file(absolutePath: { regex: "/1.jpg/" }) {
        childImageSharp {
          fixed(width: 710) {
            ...GatsbyImageSharpFixed
          }
        }
      }
      imgTwo: file(absolutePath: { regex: "/2.jpg/" }) {
        childImageSharp {
          fixed(width: 710) {
            ...GatsbyImageSharpFixed
          }
        }
      }
    }
  `)



  return (
    <div>
      <Image
      fixed={data.imgOne.childImageSharp.fixed}
      />


    <div id="secondImageHome">
      <Image

        fixed={data.imgTwo.childImageSharp.fixed}
      />
    </div>

    </div>
  )
}

export default ImagesHome

I would use the export component image in single code like:

<Img fluid={props.data.imageOne.childImageSharp.fluid} />
<Img fluid={props.data.imageTwo.childImageSharp.fluid} />

Will it be possible with my code? I tried some tutorials on YouTube, but it did not work.

Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
drugo12
  • 41
  • 2
  • it should work. did u try using relative path like https://stackoverflow.com/a/48141627/6141587? – deadcoder0904 May 10 '20 at 09:47
  • Hi, welcome to StackOverflow! I'm having trouble understanding what you're trying to achieve, and what the error you're getting actually is. Could you add the error to your question? One thing I'm already noticing is that you're mixing fluid and fixed images (fixed in the first snippet, fluid in the second). Is it intended? [See the plugin docs for the difference between the two](https://www.gatsbyjs.org/packages/gatsby-image/#two-types-of-responsive-images) – Robin Métral May 11 '20 at 11:51

0 Answers0