How do I query multiple images with gatsby-image?

后端 未结 4 1814
遥遥无期
遥遥无期 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:38

    Here is a simple example with TypeScript and SVG support:

    Update gatsby-config.js

    module.exports = {
      plugins: [
        {
          resolve: `gatsby-source-filesystem`,
          options: {
            name: `images`,
            path: `${__dirname}/src/assets/images`,
          },
        },
      ],
    };
    

    Create Image Component

    import * as React from 'react';
    import { FC } from 'react';
    import { graphql, StaticQuery } from 'gatsby';
    import Img from 'gatsby-image';
    
    interface IProps {
      name: string;
      alt: string;
      className?: string;
    }
    
    const Image: FC = ({ name, alt, className }) => (
       {
          const isNameWithSVGExtension = name.indexOf('svg') !== -1;
    
          const renderImageWithSVGExtension = () => {
            const image = allImagesWithSVGExtension.nodes.find(
              ({ publicURL }) => publicURL && publicURL.indexOf(name) !== -1
            );
            return image ? (
              {alt}
            ) : null;
          };
    
          const renderImageWithoutSVGExtension = () => {
            const image = allImagesWithoutSVGExtension.nodes.find(
              ({ publicURL }) => publicURL && publicURL.indexOf(name) !== -1
            );
            return image && image.sharp && image.sharp.fluid ? (
              {alt}
            ) : null;
          };
    
          return isNameWithSVGExtension
            ? renderImageWithSVGExtension()
            : renderImageWithoutSVGExtension();
        }}
      />
    );
    
    export { Image };
    
    

    Use Image component as

    compony logo
    or
    compony logo
    

提交回复
热议问题