gpt4 book ai didi

javascript - Gatsby: img 是一个空元素标签,不能有 `children` 也不能使用 `dangerouslySetInnerHTML`

转载 作者:行者123 更新时间:2023-11-30 19:24:25 30 4
gpt4 key购买 nike

我正在尝试使用 Gatsby Js 构建一个包含导入图像的组件。但是,当添加带有 anchor HTML 标记的链接时,我收到了 img is a void element tag and must neither have child nor use dangerouslySetInnerHTML`。

没有<a> ... </a>没有这样的错误。

我的代码:

import React from "react"
import medium from "../images/medium.png"


const Socials = () => (
<div>
<a href="https://medium.com"> <img src={medium}> </img> </a>
</div>
)
export default Socials

堆栈跟踪:

  in img (at Socials.js:8)
in a (at Socials.js:8)
in div (at Socials.js:7)
in Socials (at pages/index.js:44)
in IndexPage (created by HotExportedIndexPage)
in AppContainer (created by HotExportedIndexPage)
in HotExportedIndexPage (created by PageRenderer)
in PageRenderer (created by JSONStore)
in JSONStore (created by RouteHandler)
in RouteHandler (created by EnsureResources)
in div (created by FocusHandlerImpl)
in FocusHandlerImpl (created by Context.Consumer)
in FocusHandler (created by RouterImpl)
in RouterImpl (created by Context.Consumer)
in Location (created by Context.Consumer)
in Router (created by EnsureResources)
in ScrollContext (created by EnsureResources)
in RouteUpdates (created by EnsureResources)
in EnsureResources (created by LocationHandler)
in LocationHandler (created by LocationProvider)
in LocationProvider (created by Context.Consumer)
in Location (created by Root)
in Root
in _default

将 URL 链接添加到图像的正确方法是什么?

最佳答案

您应该能够将图像导入您的组件,并像这样将其添加到图像 src。

import React from "react"
import gatsbyIconImage from "../images/gatsby-icon.png"

const IndexPage = () => (
<>
<a href="https://stackoverflow.com"><img alt="stack overflow" src={gatsbyIconImage}></img></a>
</>
)

export default IndexPage

但是,如果您在开头和结尾之间放置任何内容 <img>标签(即使只是一个空格), Gatsby 似乎认为它有子元素并且不会正确编译。

关于javascript - Gatsby: img 是一个空元素标签,不能有 `children` 也不能使用 `dangerouslySetInnerHTML`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57073650/

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