gpt4 book ai didi

Gatsby Img 不渲染

转载 作者:行者123 更新时间:2023-12-04 09:16:19 25 4
gpt4 key购买 nike

我正在尝试使用以下页面上的 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 对象。
日志
Console log
我错过了一些非常明显的东西吗?
我有 Gatsby-image 在站点的其他部分工作,因此配置没有问题。

最佳答案

你的父组件是一个 flexbox。可能您必须为图像组件指定高度和宽度:添加 style={{height: "100px", width: "100px"}}Img为了看到它。
解释:
具有默认增长和收缩值的 flexbox 容器扩展到最小尺寸以显示其子组件。因为 Gatsby-Image 组件的默认最小尺寸未设置,所以它被隐式设置为 0。这就是为什么您看不到任何东西但您的开发人员工具仍会显示 HTML 节点的原因。
通过声明宽度和高度,您可以设置在浏览器中可以看到的最小尺寸。

关于 Gatsby Img 不渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63194512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com