Skip to content

Instantly share code, notes, and snippets.

@byurhannurula
Last active July 15, 2019 17:27
Show Gist options
  • Save byurhannurula/df7eabe5738e3a6476fc16fc9e5d6f9f to your computer and use it in GitHub Desktop.
Save byurhannurula/df7eabe5738e3a6476fc16fc9e5d6f9f to your computer and use it in GitHub Desktop.
Gatsby Image component
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
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