【发布时间】:2020-02-20 09:14:02
【问题描述】:
我正在尝试通过构建一个演示博客来学习 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>
)
}
【问题讨论】:
-
代码没问题,请详细说明图片位置,并做一个可生产的例子How to create a Minimal, Reproducible Example
-
图像位于“/src/images”文件夹中。我在 Markdown 文件中定义了图像路径
-
所以给我们看,给我们看markdown,请阅读我分享的链接
-
另外,向我们展示您记录的数据值
-
请显示您记录的数据,您是否在 GraphiQL 中检查过您的查询?
标签: reactjs graphql gatsby gatsby-image