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
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 ? (
) : null;
};
const renderImageWithoutSVGExtension = () => {
const image = allImagesWithoutSVGExtension.nodes.find(
({ publicURL }) => publicURL && publicURL.indexOf(name) !== -1
);
return image && image.sharp && image.sharp.fluid ? (
) : null;
};
return isNameWithSVGExtension
? renderImageWithSVGExtension()
: renderImageWithoutSVGExtension();
}}
/>
);
export { Image };
Use Image component as
or