Created
August 13, 2019 20:08
-
-
Save ryanscherler/a5f3b334ad22e9d60f7c0a8091b2ea54 to your computer and use it in GitHub Desktop.
Gatsby Dynamic Image
This file contains hidden or 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
import React from 'react' | |
import PropTypes from 'prop-types' | |
import Img from 'gatsby-image' | |
const Image = ({ node, alt, className }) => { | |
if (node.childImageSharp && node.childImageSharp.fluid) { | |
return ( | |
<Img fluid={node.childImageSharp.fluid} alt={alt} className={className} /> | |
) | |
} | |
if (node.childImageSharp && node.childImageSharp.fixed) { | |
return ( | |
<Img fixed={node.childImageSharp.fixed} alt={alt} className={className} /> | |
) | |
} | |
return node.publicURL ? ( | |
<img src={node.publicURL} alt={alt} className={className} /> | |
) : null | |
} | |
Image.propTypes = { | |
node: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired, | |
alt: PropTypes.string, | |
className: PropTypes.string, | |
} | |
Image.defaultProps = { | |
alt: '', | |
} | |
export default Image |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment