0

I'm trying to learn Gatsby by building a demo blog. I have an Archive component, where I'm querying blog data, and I'm trying to pass the Image down as a prop, however, when im trying to access the prop within my Article component, I keep getting a TypeError: Cannot read property 'childImageSharp' of null.

Here is my code:

import React from "react"
import { useStaticQuery, graphql } from "gatsby"
import Img from "gatsby-image"

// components
import Article from "./article"

const BLOG_QUERY = graphql`
  {
    allMarkdownRemark {
      nodes {
        excerpt
        frontmatter {
          title
          date(formatString: "MMMM DD, YYYY")
          author
          slug
          image {
            childImageSharp {
              fluid(maxWidth: 600) {
                ...GatsbyImageSharpFluid_withWebp_tracedSVG
              }
            }
          }
        }
        timeToRead
      }
    }
  }
`
const Archive = () => {
  const data = useStaticQuery(BLOG_QUERY)
  console.log(data)
  return (
    <div>
      {data.allMarkdownRemark.nodes.map(node => (
        <Article
          key={node.frontmatter.slug}
          title={node.frontmatter.title}
          excerpt={node.excerpt}
          image={node.frontmatter.image.childImageSharp.fluid}
        />
      ))}
    </div>
  )
}

export default Archive

Markdown header as below:

---
slug: "/what-is-gatsby"
title: "What Is Gatsby?"
image: "../images/gatsby.png"
author: "Joshua Isaac"
date: "2019-10-23"
---

Data logged: enter image description here

Article Components:

import { React } from 'react'
import Img from 'gatsby-image'

const Article = (props) => {
 return(
  <div>
    <h3>{props.frontmatter.title}</h3>
    <Img fluid={props.image} />
  </div>

 )
}
juicy j
  • 183
  • 5
  • 20

2 Answers2

0

Ensure that each of your markdown files has an image property in the frontmatter that contains a relative path to an image.

Also, in <Article/> it looks like you are trying to access a non-existant property: props.frontmatter.title.

You only pass key, title, excerpt & image props to <Article/>, and so the prop frontmatter simply does not exist.

// article.js

import { React } from 'react'
import Img from 'gatsby-image'

const Article = (props) => {
 return(
  <div>
    <h3>{props.title}</h3>
    <Img fluid={props.image} />
  </div>
 )
}
ksav
  • 20,015
  • 6
  • 46
  • 66
  • The title is also part of the frontmatter, how am i able to access the title but not image if they are both within frontmatter? – juicy j Oct 24 '19 at 13:23
  • sorry @ksav, im a bit consufed, could you elaborate? thanks in advance! – juicy j Oct 24 '19 at 13:28
0

Maybe it will be useful to someone:

  1. Install npm install --save gatsby-source-filesystem

  2. Add to your gatsby-config.js:

    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `static`,
        path: `${__dirname}/static/blog/`,
      },
    },
  1. Create a folder static next to src

  2. Change the path to your image in the markdown file

  3. And than:

import React from "react"
import { useStaticQuery, graphql } from "gatsby"

// components
import Article from "./article"

const BLOG_QUERY = graphql`
  {
    allMarkdownRemark {
      nodes {
        excerpt
        frontmatter {
          title
          date(formatString: "MMMM DD, YYYY")
          author
          slug
          image {
            childImageSharp {
               gatsbyImageData(
                 formats: [AUTO, WEBP, AVIF]
               )
            }
          }
        }
        timeToRead
      }
    }
  }
`
const Archive = () => {
  const data = useStaticQuery(BLOG_QUERY)
  console.log(data)
  return (
    <div>
      {data.allMarkdownRemark.nodes.map(node => (
        <Article
          key={node.frontmatter.slug}
          title={node.frontmatter.title}
          excerpt={node.excerpt}
          image={node.frontmatter.image}
        />
      ))}
    </div>
  )
}

export default Archive
import { React } from 'react'
import { GatsbyImage, getImage } from "gatsby-plugin-image"

const Article = (props) => {

const image = getImage(props.image)

 return(
  <div>
    <h3>{props.title}</h3>
    <GatsbyImage image={image} alt={props.title} />
  </div>

 )
}
  1. If it doesn't work: gatsby clean
Piotr
  • 510
  • 6
  • 8