将盖茨比图像作为道具传递

时间:2019-10-24 05:02:23

标签: reactjs graphql gatsby gatsby-image

我正在尝试通过构建演示博客来学习盖茨比。我有一个Archive组件,我在其中查询博客数据,并试图将Image作为道具传递下来,但是,当我尝试在Article组件中访问该道具时,我不断收到{{1} }。

这是我的代码:

TypeError: Cannot read property 'childImageSharp' of null

Markdown标头如下:

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

记录的数据: enter image description here

文章组成部分:

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

2 个答案:

答案 0 :(得分:0)

请确保您的每个降价文件在前题中都有一个image属性,其中包含图像的相对路径。

此外,在<Article/>中,您似乎正在尝试访问不存在的属性:props.frontmatter.title

您仅将key, title, excerpt & image个道具传递给<Article/>,因此道具frontmatter根本不存在。

// 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>
 )
}

答案 1 :(得分:0)

也许对某人有用:

  1. 安装npm install --save gatsby-source-filesystem

  2. 添加到您的 gatsby-config.js:

    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `static`,
        path: `${__dirname}/static/blog/`,
      },
    },
  1. 在src旁边创建一个static文件夹

  2. 更改 Markdown 文件中图片的路径

  3. 然后:

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. 如果它不起作用:gatsby clean