Last active
July 15, 2019 17:27
-
-
Save byurhannurula/df7eabe5738e3a6476fc16fc9e5d6f9f to your computer and use it in GitHub Desktop.
Gatsby Image component
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
import React from 'react' | |
import Img from 'gatsby-image' | |
import { graphql, useStaticQuery } from 'gatsby' | |
const query = graphql` | |
query { | |
images: allFile(filter: { extension: { regex: "/jpeg|jpg|png|gif/" } }) { | |
edges { | |
node { | |
extension | |
relativePath | |
childImageSharp { | |
fluid(maxWidth: 768) { | |
...GatsbyImageSharpFluid_withWebp_tracedSVG | |
} | |
} | |
} | |
} | |
} | |
} | |
` | |
const Image = ({ src, ...props }) => { | |
const { images } = useStaticQuery(query) | |
const file = images.edges.find(image => image.node.relativePath === src) | |
return <Img fluid={file.node.childImageSharp.fluid} {...props} /> | |
} | |
export default Image |
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
import React from 'react' | |
import Layout from 'components/layout' | |
import Image from 'components/image' | |
const IndexPage = () => ( | |
<Layout> | |
<h1>Hi people</h1> | |
<Image src="images/me.jpg" /> | |
</Layout> | |
) | |
export default IndexPage |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment