I have hero image that I want to query for multiple times and fetch it in different sizes so I can use it in different devices sizes.
My hero image is in
src/images/hero.png
This is the query code:
export const mobileHeroImage = graphql`
fragment mobileHeroImage on File {
childImageSharp {
fluid(maxWidth: 375, maxHeight: 400) {
...GatsbyImageSharpFluid
}
}
}
`;
export const query = graphql`
query {
mobileHeroImage: file(relativePath: { eq: "hero.png" }) {
...mobileHeroImage
}
}
`;
This is my component's code in index.js:
const IndexPage = ({ data }) => {
// First console log
console.log(data);
// Second console log
console.log(getImage(data.mobileHeroImage));
First console log logs the object with the image object:
The second console log logs undefined even though there is mobileHeroImage
object inside of data
The image in multiple dimensions would be passed as an array like this:
export function MyImage({ data }) {
const images = withArtDirection(getImage(data.mobileHeroImage), [
// I would add more sizes here for different screen sizes
{
media: "(max-width: 1024px)",
image: getImage(data.smallImage),
},
])
return <GatsbyImage image={images} />
}