Skip to content

Instantly share code, notes, and snippets.

@sloyer
Created April 16, 2021 21:52
Show Gist options
  • Save sloyer/83416d9ed40881044b9901d7483b776f to your computer and use it in GitHub Desktop.
Save sloyer/83416d9ed40881044b9901d7483b776f to your computer and use it in GitHub Desktop.
getImg
/* eslint-disable */
import React from 'react'
// Get image from a collection, basically we need to abstract when using the useGetImg hook in a loop thanks to rules of hooks
const getImg = (id, collection) => {
let img = {}
let pos = collection.map((item) => { return item.databaseId }).indexOf(id)
if (pos > -1) {
img.src = collection[pos].sourceUrl
img.srcSet = collection[pos].srcSet
// img.sizes = media.allWpMediaItem.nodes[pos].sizes
img.sizes = "(max-width: 800px) 100w, 50vw"
img.alt = collection[pos].altText
} else {
img = null
}
return img
}
export default getImg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment