Images
🔔️ Important
Query: is used for files under pages/ directories.
StaticQuery: is used for files under components/ or any other directory. More info...
useStaticQuery: Instead of StaticQuery, you can use the hook useStaticQuery, which is easier. More info...
Query: is used for files under pages/ directories.
StaticQuery: is used for files under components/ or any other directory. More info...
useStaticQuery: Instead of StaticQuery, you can use the hook useStaticQuery, which is easier. More info...
With Gatsby, we can make images to load faster by optimized handling and sizing.
- Install
gatsby-imageand other, necessary dependencies likegatsby-plugin-sharp,gatsby-transformer-sharpandgatsby-source-filesystem
$ npm i gatsby-image gatsby-transformer-sharp gatsby-plugin-sharp gatsby-source-filesystem
- Add the newly installed plugins and transformer plugins to your
gatsby-config.js
module.exports = {plugins: [`gatsby-plugin-sharp`, `gatsby-transformer-sharp`],}
- Configure
gatsby-source-filesystemto load images from a folder. In order to use GraphQL to query the image files, the files need to be in a location that is known to Gatsby. This requires an update togatsby-config.jsto configure the plugin. Feel free to replace thepathoption with wherever your images are located relative to your project.
module.exports = {plugins: [`gatsby-transformer-sharp`,`gatsby-plugin-sharp`,{resolve: `gatsby-source-filesystem`,options: {path: `${__dirname}/src/images/`,}}],}
- Write a GraphQL query using one of the included GraphQL “fragments” which specify the fields needed by
gatsby-imageto create a responsive, optimized image. This example will useGatsbyImageSharpFluid. An example of a GraphQL query is below where the path listed is the path relative to the location specified in thegatsby-source-filesystemoptions.
file(relativePath: { eq: "default.jpg" }) {childImageSharp {# Specify the image processing specifications right in the query.fluid {...GatsbyImageSharpFluid}}}
- Import
Imgto display the fragment in JSX. There are additional features available with theImgtag as well.
import Img from "gatsby-image"export default ({ data }) => (<div><h1>Hello gatsby-image</h1><Imgfluid={data.file.childImageSharp.fluid}alt="Gatsby Docs are awesome"/></div>)
This GraphQL query creates multiple sizes of the image and when the page is rendered the image that is appropriate for the current screen resolution (e.g. desktop, mobile, and everything in between) is used. The gatsby-image component automatically enables a blur-up effect as well as lazy loading images that are not currently on screen.
Quering several image files
import React from "react"import { StaticQuery, graphql } from "gatsby"import Img from "gatsby-image"const MyComponent = () => (<StaticQueryquery = {graphql`query {image1: file(relativePath: {eq: "image1.jpeg"}) {childImageSharp {fluid (maxWidth: 500) {...GatsbyImageSharpFluid_withWebp}}},image2: file(relativePath: {eq: "image2.jpg"}) {childImageSharp {fluid (maxWidth: 500) {...GatsbyImageSharpFluid_withWebp}}},image3: file(relativePath: {eq: "image3.jpeg"}) {childImageSharp {fluid (maxWidth: 500) {...GatsbyImageSharpFluid_withWebp}}}}`}render={data => (<section id="services"><Img fluid={data.image1.childImageSharp.fluid} /><Img fluid={data.image2.childImageSharp.fluid} /><Img fluid={data.image3.childImageSharp.fluid} /></section>)}/>)export default MyComponent
More info at:
Importing small icon images (png, svg)
It is advised not to use GraphQL queries, as small images lose plenty of definition.
/components/images.js
import html from '../images/svg/html.svg'import css from '../images/svg/css.svg'import javascript from '../images/svg/javascript.svg'export {html, css, javascript}
technologies.js
import React from "react"import {html, css, javascript} from '../components/images'const Technologies = () =><div className="technologies"><img src={html} alt="html"/><img src={css} alt="css"/><img src={javascript} alt="javascript"/></div>export default Technologies;