Member-only story
Gatsby.js — Display a Single Image
3 min readFeb 6, 2021
Gatsby is a static web site framework that’s based on React.
We can use it to create static websites from external data sources and more.
In this article, we’ll look at how to create a site with Gatsby.
Get and Display a Single Image
We can get and display a single image with Gatsby.
We use the gatsby-image
to get and add an image.
To install the packages required packages, we run:
npm i gatsby-plugin-sharp gatsby-transformer-sharp
To do this, we write:
gatsby-config.js
const path = require('path');module.exports = {
plugins: [
{
resolve: `gatsby-source-filesystem`,
options: {
name: `images`,
path: path.join(__dirname, `src`, `images`),
},
},
`gatsby-plugin-sharp`,
`gatsby-transformer-sharp`,
],
}
Then we can get the image on our page by writing:
src/pages/index.js
import React from "react"
import { useStaticQuery, graphql } from "gatsby"
import Img from "gatsby-image"const IndexPage = () => {
const data = useStaticQuery(graphql`
query {…