0

I've been trying for hours to figure out how to get a background image component using GatsbyJS, I've tried multiple different ways to skin the cat but nothing I've tried seems to work. I'm not sure what I'm missing

I'm importing my BackgroundImage component, I'm running graphQL to get the filtered image from my images folder (graphQL is returning the correct image) I'm passing the data into the page and destructuring it

my index.js

import * as React from 'react'
import Layout from '../components/layout'
import Seo from '../components/seo'
import { Contact } from '../components/contactMe'
import { Link } from 'gatsby'
import { BackgroundImage } from '../components/backgroundImage'

// Step 2: Define your component
const IndexPage = ({ data }) => {
  return (
    <main>
      <Layout>
          <BackgroundImage imageData={data.backgroundImage.childImageSharp.gatsbyImageData}>
          <h1>Welcome to Our Site</h1>
          <p>This is the homepage with a custom background image.</p>
        </BackgroundImage>
         <Contact />
      </Layout>
    </main>
  )
}


export const query = graphql`
query {
  backgroundImage: file(relativePath: { eq: "process-background.webp" }) {
    childImageSharp {
      gatsbyImageData(layout: FULL_WIDTH, placeholder: BLURRED)
    }
  }
}
`



export const Head = () => <Seo title="Home Page" />

// Step 3: Export your component
export default IndexPage

I'm importing GatsbyImage and GetImage I'm creating a const to store the value the structure looks correct I'm not sure what I'm missing

my component

import { GatsbyImage, getImage } from "gatsby-plugin-image"

const BackgroundImage = ({ children, imageData }) => {
    const image = getImage(imageData)
  return (
    <div>
      <GatsbyImage
        src={image} // Dynamically pass the image path here
        alt="Background Image"
        style={{
          position: "absolute",
          top: 0,
          left: 0,
          width: "100%",
          height: "100%",
        }}
        imgStyle={{
          objectFit: "cover", // Make the image cover the entire area
        }}
        layout="fullWidth"  // This ensures the image covers the entire section
        placeholder="blurred" // Optional: adds a blurred placeholder during loading
      />
      <div className="background-content">{children}</div>
    </div>
  )
}

export default BackgroundImage

the error message I'm getting is the following

One unhandled runtime error found in your files. See the list below to fix it:

Error in function IndexPage in ./src/pages/index.js:89 Cannot read properties of undefined (reading 'backgroundImage')

./src/pages/index.js:89 Open in Editor 87 | 88 |

89 | | ^ 90 |

Welcome to Our Site

91 |

This is the homepage with a custom background image.

92 |

9
  • Can you confirm backgroundImage is there in the data BackgroundImage imageData={data.backgroundImage.childImageSharp.gatsbyImageData}> Commented Nov 7, 2024 at 2:04
  • when doing a console.log(data) the console is saying undefined, however, graphQL is showing the correct image. Commented Nov 7, 2024 at 2:39
  • in your index.js console the rest of the props too check out what is coming Commented Nov 7, 2024 at 5:58
  • I've added this to the index.js const IndexPage = (props) => { console.log(props); // This will log the full props object, including data if available. const { data } = props; // Extract data for easier access but I'm not seeing data or props in the console Commented Nov 7, 2024 at 15:52
  • I've also added const IndexPage = ({ data }) => { console.log('Data:', data); const { data } = props; // Extract data for easier access but I'm getting Data: undefined in the console Commented Nov 7, 2024 at 16:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.