Skip to content

Instantly share code, notes, and snippets.

@fourjuaneight
Created July 30, 2019 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fourjuaneight/44909492dda6bb64f3bcd3b5d3cd0902 to your computer and use it in GitHub Desktop.
Save fourjuaneight/44909492dda6bb64f3bcd3b5d3cd0902 to your computer and use it in GitHub Desktop.
gatsby-image Component
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