How do I query multiple images with gatsby-image?

后端 未结 4 1811
遥遥无期
遥遥无期 2021-02-05 19:05

I have 16 images that I want to render out onto a website in a grid format.

I\'m using the following plugins for this:

  • gatsby-image
  • <
4条回答
  •  孤街浪徒
    2021-02-05 19:41

    The easiest way is to create an image provider:

    import React from 'react'
    import { graphql, useStaticQuery } from 'gatsby'
    import Img from 'gatsby-image'
    
    const Image = ({ fileName, alt, style }) => {
      const { allImageSharp } = useStaticQuery(graphql`
        query {
          allImageSharp {
            nodes {
              fluid(maxWidth: 1600) {
                originalName
                ...GatsbyImageSharpFluid_withWebp
              }
            }
          }
        }
      `)
    
      const fluid = allImageSharp.nodes.find(n => n.fluid.originalName === fileName)
        .fluid
    
      return (
        
    {alt}
    ) } export default Image;

    And then, after importing, easily insert the image which you need:

    
    

提交回复
热议问题