Didn't find this on the site but I did find an open bug on Github and the only resolution, at time of writing, is to use GatsbyImage
. Learning to convert a Gatsby project from 2 to 3 I've installed gatsby-plugin-image and am converting a component that uses a non-changing image in a Hero component and per the docs StaticImage
should work.
The old component:
import React from 'react'
import { graphql, useStaticQuery } from 'gatsby'
import Image from 'gatsby-image'
const query = graphql`
{
person: file(relativePath: {eq: "person.png"}) {
childImageSharp {
fluid {
...GatsbyImageSharpFluid
}
}
}
}
`
const Hero = ({ showPerson }) => {
const { person } = useStaticQuery(query)
return (
<header className="hero">
{showPerson && <Image fluid={person.childImageSharp.fluid} className="hero-person" />}
</header>
)
}
export default Hero
new component:
import React from 'react'
import { StaticImage } from 'gatsby-plugin-image'
import personImage from '../assets/person.png'
const Hero = ({ showPerson }) => {
console.log(personImage)
return (
<header className="hero">
{showPerson && <StaticImage src={personImage} className="hero-person" alt="person image" />}
</header>
)
}
export default Hero
When I log the asset I get (no issues with my file path):
Hero.js:7 /static/person-c7035ca6b9544d80f8f0626ea3e22ace.png
but the log renders:
react_devtools_backend.js:2430 No data found for image "undefined"
Image not loaded /static/person-c7035ca6b9544d80f8f0626ea3e22ace.png
and in the terminal I get:
"gatsby-plugin-image" threw an error while running the preprocessSource lifecycle:
Cannot read property 'startsWith' of undefined
With Gatsby image StaticImage
is there a way to render an image that doesn't change in a component without using GatsbyImage
?