I'm working on a Gatsby project and I've run into a frustrating problem. I have a component that receives a relative path to an image as props, which I then use to populate a background-image property:
import React, { FC } from 'react'
// Prop typings
type TProps = {
imageUrl: string;
}
const MyComponent: FC<TProps> = ({ imageUrl }) => {
const divStyles = {
backgroundImage: `url(${imageUrl})`,
}
return (
<div style={divStyles}></div>
)
}
export default MyComponent
The relative path passed in imageUrl references an image in my src directory. I've double-checked that the path is correct and the image exists, yet the image won't appear when the component renders.
I can set the background image successfully using CSS, but it fails when the image path is passed as a prop. I've also tried setting the image URL using Gatsby's withPrefix utility function, but no dice.
Am I doing something wrong, or is it just impossible to define a background image in Gatsby from props?