So I created a reusable hero section in React and I retrieve the images from a data file, so all I need to do to update the new image is I just update my data file. I'm trying to convert this component into Gatsby, but I'm not sure how to implement their images plugin with my data file.
My image component returns all my images with this code rn
const Image = () => {
const data = useStaticQuery(graphql`
query {
allImageSharp {
edges {
node {
id
fluid(maxWidth: 200, maxHeight: 200) {
...GatsbyImageSharpFluid
}
}
}
}
}
`)
return (
<>
{data.allImageSharp.edges.map(edge => (
<Img fluid={edge.node.fluid} />
))}
</>
)
}
Below is my React code that I'm trying to convert to use gatsby
My data file is just an object that links the image I want to use
export const heroSectionOne = {
img: require('../../images/profile.jpg'),
alt: 'Image',
start: 'true'
};
export const heroSectionTwo = {
img: require('../../images/house.jpg'),
alt: 'Image',
start: 'true'
};
For now, I just pass in the props on the component
<ImgWrapper start={start}>
<Img src={img} alt={alt} />
</ImgWrapper>
Then in my home page component, I will reuse the component, but switch which data file is used, so I get a different image
<InfoSection {...heroSectionOne} />
<InfoSection {...heroSectionTwo} />
So right now, my component will display img '../../images/profile.jpg'
and the second section will display the house.jpg picture because I have it hard coded in my data file, but with Gatsby, how would I replicate this same method with their image component?
How would I write my image component in gatsby to be able to just pass in the image component anywhere in my app and then add whichever image I want to use in the component I end up adding it to?
I've only seen tutorials showing how to add a specific image to your query or how to display all your images in your folder at one time, but haven't seen anything about passing it through a data file