Created
April 16, 2021 21:52
-
-
Save sloyer/83416d9ed40881044b9901d7483b776f to your computer and use it in GitHub Desktop.
getImg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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