我正在尝试使用Gatsby Js构建一个组件,其中包含导入的图像。 然而,当添加一个带有锚HTML标记的链接时,我会收到错误信息: img是无元素标签,既不能拥有子元素
也不能使用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链接的正确方法是什么?
<img />
而不是<img></img>
。 - Andreas<a href="https://medium.com"> <img src={medium} /> </a>
,并收到了相同的错误信息。 - isaacsultan