Need help to use GatsbyImage in react-photo-album #76
|
I'm having hard time to figure out how to incorporate GatsbyImage component into my project, in Gatsby all local images have prepared in the {
"name": "DSC07476",
"childImageSharp": {
"gatsbyImageData": {
"layout": "constrained",
"backgroundColor": "#484848",
"images": {
"fallback": {
"src": "/static/cce539c7352bb03422c04981baf38131/9643b/DSC07476.jpg",
"srcSet": "/static/cce539c7352bb03422c04981baf38131/d309f/DSC07476.jpg 400w,\n/static/cce539c7352bb03422c04981baf38131/0f2ec/DSC07476.jpg 800w,\n/static/cce539c7352bb03422c04981baf38131/9643b/DSC07476.jpg 1600w",
"sizes": "(min-width: 1600px) 1600px, 100vw"
},
"sources": [
{
"srcSet": "/static/cce539c7352bb03422c04981baf38131/40bf3/DSC07476.webp 400w,\n/static/cce539c7352bb03422c04981baf38131/73d40/DSC07476.webp 800w,\n/static/cce539c7352bb03422c04981baf38131/a6357/DSC07476.webp 1600w",
"type": "image/webp",
"sizes": "(min-width: 1600px) 1600px, 100vw"
}
]
},
"width": 1600,
"height": 1782
}
}and here is the code to render GatsbyImages: {images.nodes.map((image) => (
<GatsbyImage key={image.name} alt={image.name} image={image.childImageSharp.gatsbyImageData} />
))}So it looks like I should manually structure the |
Answered by
igordanchenko
Jul 20, 2022
Replies: 1 comment 4 replies
|
Since you are using a custom render function in this case, you can pass any string as photo src, as long as you are able to lookup the photo object based on that string inside the render function. For example, you can pass an index of the photo in the array. const photos = images.nodes.map(
(
{
childImageSharp: {
gatsbyImageData: { width, height },
},
},
index
) => ({
src: `${index}`,
width,
height,
})
);and then lookup Gatsby image in the renderPhoto: const image = images.nodes[+photo.src];
return <GatsbyImage key={image.name} alt={image.name} image={image.childImageSharp.gatsbyImageData} /> |
4 replies
Answer selected by
igordanchenko
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since you are using a custom render function in this case, you can pass any string as photo src, as long as you are able to lookup the photo object based on that string inside the render function.
For example, you can pass an index of the photo in the array.
and then lookup Gatsby image in the renderPhoto: