Created
July 30, 2019 21:48
-
-
Save fourjuaneight/44909492dda6bb64f3bcd3b5d3cd0902 to your computer and use it in GitHub Desktop.
gatsby-image Component
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 Img from 'gatsby-image'; | |
import PropTypes from 'prop-types'; | |
import React from 'react'; | |
import { StaticQuery, graphql } from 'gatsby'; | |
const Image = props => ( | |
<StaticQuery | |
query={graphql` | |
query { | |
allImageSharp { | |
edges { | |
node { | |
fluid(maxWidth: 100) { | |
...GatsbyImageSharpFluid | |
originalName | |
} | |
} | |
} | |
} | |
} | |
`} | |
render={data => { | |
const image = data.allImageSharp.edges.find(edge => | |
edge.node.fluid.originalName.includes(props.filename) | |
); | |
if (!image) { | |
return null; | |
} | |
return ( | |
<Img | |
fluid={image.node.fluid} | |
alt={props.alt} | |
style={props.style} | |
imgStyle={props.imgStyle} | |
/> | |
); | |
}} | |
/> | |
); | |
Image.propTypes = { | |
alt: PropTypes.string.isRequired, | |
filename: PropTypes.string.isRequired, | |
imgStyle: PropTypes.object, | |
style: PropTypes.object, | |
}; | |
export default Image; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment