Gatsby Img 不渲染

Gatsby Img not rendering

我正在尝试使用下一页的 Gatsby Img 组件加载图片:

import React from "react"
import { graphql, useStaticQuery } from "gatsby"
import Img from "gatsby-image"
import Layout from "../components/layout"
import SEO from "../components/seo"
import FeatureCard from "../components/featureCard"
import { getImage } from "../utils/getImage"

function IndexPage() {
  const data = useStaticQuery(graphql`
      query {
          file(relativePath: { eq: "index/profile.JPG" }) {
              childImageSharp {
                  fluid {
                      ...GatsbyImageSharpFluid
                  }
              }
          }
      }
  `)

  console.log(data.file.childImageSharp.fluid);


  const featureCardContainer =
    "flex justify-center w-full my-2 md:px-2 lg:my-4 lg:px-4 lg:w-full xl:w-1/2"

  return (
    <Layout>

      <div className="w-full h-auto flex flex-col justify-center items-center">

        <h1 className="text-2xl">Testing</h1>
        <Img fluid={data.file.childImageSharp.fluid} alt={'Profile Picture'} />

      </div>
    </Layout>
  )
}

export default IndexPage

Img 组件未呈现,我不明白为什么。我已经记录了 graphql 查询,它确实在获取正确的 GatsbyImageSharpFluid 对象。

日志

我是不是漏掉了一些很明显的东西?

我在网站的其他部分使用了 Gatsby-image,所以这不是配置问题。

您的父组件是一个 flexbox。可能是您必须为图像组件指定高度和宽度:将 style={{height: "100px", width: "100px"}} 添加到 Img 才能看到它。

解释:

具有默认增长和收缩值的 flexbox 容器扩展到最小尺寸以显示其子组件。因为没有设置 Gatsby-Image 组件的默认最小大小,它被隐式设置为 0。这就是为什么你看不到任何东西,但你的开发人员工具仍然会显示 HTML 节点。

通过声明宽度和高度,您可以设置可以在浏览器中看到的最小尺寸。