Gatsby: img is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML`(Gatsby:img是一个空元素标签,既不能有`Children`,也不能使用`DangerouslySetInnerHTML`)
本文介绍了Gatsby:img是一个空元素标签,既不能有`Children`,也不能使用`DangerouslySetInnerHTML`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Gatsby Js构建一个组件,其中包括导入的图像。但是,当使用锚Html标记添加链接时,我收到的是img is a void element tag and must neither have
childrennor 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链接添加到图像的正确方法是什么?
推荐答案
您应该能够将图像导入到组件中,并将其添加到图像源中,如下所示。
import React from "react"
import gatsbyIconImage from "../images/gatsby-icon.png"
const IndexPage = () => (
<>
<a href="https://stackoverflow.com"><img alt="c3RhY2sgb3ZlcmZsb3c=" src={gatsbyIconImage}></img></a>
</>
)
export default IndexPage
但是,如果将任何内容放在开始和结束<img>
标记之间(即使只有一个空格),Gatsby似乎认为它有子元素,无法正确编译。
这篇关于Gatsby:img是一个空元素标签,既不能有`Children`,也不能使用`DangerouslySetInnerHTML`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:Gatsby:img是一个空元素标签,既不能有`Children`,也不能使用`DangerouslySetInnerHTML`


猜你喜欢
- Fetch API 如何获取响应体? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06