Skip to content

Instantly share code, notes, and snippets.

@ryanscherler
Created August 13, 2019 20:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanscherler/a5f3b334ad22e9d60f7c0a8091b2ea54 to your computer and use it in GitHub Desktop.
Save ryanscherler/a5f3b334ad22e9d60f7c0a8091b2ea54 to your computer and use it in GitHub Desktop.
Gatsby Dynamic Image
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