【发布时间】:2019-06-25 08:47:12
【问题描述】:
我已经用 gatsby.js 试验了一段时间,除了这个问题外,一切都很顺利,我不能在应用程序中包含 jQuery 脚本,以便在 gatsby 应用程序渲染后加载,我已经包含脚本标记到html.js 文件并加载它,但似乎代码是在react 将内容呈现到屏幕之前执行的@ 应用程序。但是没有运气,这是我的html.js 文件的源代码:
html.js
import React from "react"
import PropTypes from "prop-types"
export default class HTML extends React.Component {
componentDidMount() {
console.log('hello world');
}
render() {
return (
<html {...this.props.htmlAttributes}>
<head>
<meta charSet="utf-8" />
<meta httpEquiv="x-ua-compatible" content="ie=edge" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
{this.props.headComponents}
</head>
<body>
{this.props.preBodyComponents}
<div
key={`body`}
id="___gatsby"
dangerouslySetInnerHTML={{ __html: this.props.body }}
/>
{this.props.postBodyComponents}
</body>
</html>
)
}
}
HTML.propTypes = {
htmlAttributes: PropTypes.object,
headComponents: PropTypes.array,
bodyAttributes: PropTypes.object,
preBodyComponents: PropTypes.array,
body: PropTypes.string,
postBodyComponents: PropTypes.array,
}
如您所见,我替换了 componentDidMount() 方法以写入控制台,但没有阻止此方法执行。
如果有人有这方面的经验,请分享,谢谢。
【问题讨论】:
-
不应该在 } 之后和 render() 之前加逗号吗?
-
@muka.gergely 你在说什么? JS中的方法之间没有逗号。
标签: jquery reactjs gatsby jamstack