isa*_*tan 7 html javascript reactjs gatsby
我正在尝试使用 Gatsby Js 构建一个组件,其中包含导入的图像。然而,当添加带有锚 HTML 标记的链接时,我收到了img is a void element tag and must neither havechildrenangerlySetInnerHTML` nor use。
没有的<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
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪:
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
Run Code Online (Sandbox Code Playgroud)
向图像添加 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
Run Code Online (Sandbox Code Playgroud)
但是,如果您在开始和结束<img>标记之间放置任何内容(即使只是一个空格),gatsby 似乎会认为它具有子元素,并且无法正确编译。
| 归档时间: |
|
| 查看次数: |
28429 次 |
| 最近记录: |