So I am trying to get the image that is defined in my markdown post and when I use GraphQLi, the query works, but in my JS code its returning the wrong image.
import React, { ReactElement } from 'react';
import { useStaticQuery, graphql } from 'gatsby';
import { GatsbyImage } from 'gatsby-plugin-image';
export const Img = (props: { alt: string; src: string }): ReactElement => {
const { alt, src } = props;
const imageName = src.split('/').slice(-1)[0];
const data = useStaticQuery(graphql`
query GetImage($imageName: String) {
file(relativePath: { eq: $imageName }) {
childImageSharp {
gatsbyImageData
}
}
}
`);
console.log(imageName, data.file.childImageSharp.gatsbyImageData);
return <GatsbyImage alt={alt} image={data.file.childImageSharp.gatsbyImageData} />;
};
The results of the console log
I would expect the office1 image but its returning me the docker image
UPDATE 1:
So I am trying out StaticImage as it seems that the use query is not expecting variables. So I have the following code that still does not render the image at all, not even the alt text
import React, { ReactElement } from 'react';
import { StaticImage } from 'gatsby-plugin-image';
export const Img = (props: { alt: string; src: string }): ReactElement => {
const { alt, src } = props,
imageUrl = `../../images/${src}`;
console.log(imageUrl);
console.log('----');
return <StaticImage alt={alt} src={imageUrl} />;
};
Image url logs ../../images/office1.jpg. If I do the following the image renders fine
return <StaticImage alt={alt} src='../../images/office1.jpg' />;
