我正在尝试通过构建一个演示博客来学习 Gatsby。我有一个存档组件,我在其中查询博客数据,并且我试图将图像作为道具向下传递,但是,当我尝试访问我的文章组件中的道具时,我不断得到一个TypeError: Cannot read property 'childImageSharp' of null
.
这是我的代码:
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 标头如下:
---
slug: "/what-is-gatsby"
title: "What Is Gatsby?"
image: "../images/gatsby.png"
author: "Joshua Isaac"
date: "2019-10-23"
---
文章组成:
import { React } from 'react'
import Img from 'gatsby-image'
const Article = (props) => {
return(
<div>
<h3>{props.frontmatter.title}</h3>
<Img fluid={props.image} />
</div>
)
}