我有一个相对简单的问题,试图将内联脚本添加到React组件.到目前为止我所拥有的:
'use strict'; import '../../styles/pages/people.scss'; import React, { Component } from 'react'; import DocumentTitle from 'react-document-title'; import { prefix } from '../../core/util'; export default class extends Component { render() { return (); } }; People
我也尝试过:
这两种方法似乎都没有执行所需的脚本.我猜这是一件我想念的简单事.有人可以帮忙吗?
PS:忽略foobar,我有一个真正的ID实际上在使用,我不想分享.
是否要在每次渲染此组件时一次又一次地获取和执行脚本,或者只是在将此组件装入DOM时只执行一次?
也许尝试这样的事情:
componentDidMount () { const script = document.createElement("script"); script.src = "https://use.typekit.net/foobar.js"; script.async = true; document.body.appendChild(script); }
但是,如果要加载的脚本不可用作模块/包,这只是非常有用.首先,我会永远:
在npm上查找包
在我的项目中下载并安装包(npm install typekit
)
import
我需要的包裹(import Typekit from 'typekit';
)
这可能是您安装软件包的方式react
,也可能是您react-document-title
的示例,并且npm上有一个Typekit软件包.
继上面的答案,你可以这样做:
import React from 'react'; export default class Test extends React.Component { constructor(props) { super(props); } componentDidMount() { const s = document.createElement('script'); s.type = 'text/javascript'; s.async = true; s.innerHTML = "document.write('This is output by document.write()!')"; this.instance.appendChild(s); } render() { return(this.instance = el)} />; } }div被绑定
this
并且脚本被注入其中.可以在codesandbox.io上找到演示
this.instance对我不起作用,但是document.body.appendChild从Alex McMillan的回答中做到了.
您可能没有将`this.instance`绑定到render方法中的ref.我添加了一个演示链接来显示它的工作情况
3> Patrice Wrex..:我最喜欢的方法是使用React Helmet - 它是一个允许以您可能习惯的方式轻松操作文档头的组件.
例如
import React from "react"; import {Helmet} from "react-helmet"; class Application extends React.Component { render () { return (); } };... https://github.com/nfl/react-helmet
这是迄今为止最好的解决方案.
不幸的是,它无法正常工作...请参阅https://codesandbox.io/s/l9qmrwxqzq
试过这个,对我不起作用。我不建议使用react-helmet,其唯一原因是它将无法删除的额外属性注入脚本。这实际上破坏了某些脚本,并且维护者多年未对其进行修复,并拒绝https://github.com/nfl/react-helmet/issues/79
@Darkowic,我通过将`async =“ true”`添加到将jQuery添加到代码的`<script>`标签中来使您的代码正常工作。
4> 小智..:Alex Mcmillan提供的答案对我帮助最大,但对于更复杂的脚本标签却不太有用.
我略微调整了他的答案,想出了一个带有各种功能的长标签解决方案,另外还设置了"src".
(对于我的用例,脚本需要生活在头部,这也反映在这里):
componentWillMount () { const script = document.createElement("script"); const scriptText = document.createTextNode("complex script with functions i.e. everything that would go inside the script tags"); script.appendChild(scriptText); document.head.appendChild(script); }
我不明白你为什么要使用React,如果你只是将内联JS转储到页面上......?
5> scabbiaza..:如果你需要
在SSR中进行阻塞(服务器端渲染),那么一种方法
componentDidMount
将无效.您可以改用
react-safe
库.React中的代码将是:import Safe from "react-safe" // in render{ `try{Typekit.load({ async: true });}catch(e){}` }
6> 小智..:我为这个特定的案例创建了一个React组件:https://github.com/coreyleelarson/react-typekit
只需将你的Typekit Kit ID作为道具传递,你就可以了.
import React from 'react'; import Typekit from 'react-typekit'; const HtmlLayout = () => (My Example React Component
); export default HtmlLayout; 推荐阅读
如何解决《如何在Fishshell中提取子串?》经验,为你挑选了1个好方法。 ... [详细] 如何解决《在matplotlib中正确对齐标签》经验,为你挑选了1个好方法。 ... [详细] 如何解决《在JavaScript中连接自定义对象数组的字符串》经验,为你挑选了1个好方法。 ... [详细] 如何解决《有没有更好的方法来构建我的NPM脚本,使它们更具可读性?》经验,为你挑选了0个好方法。 ... [详细] 如何解决《将节点代码库迁移到TypeScript:全局范围?》经验,为你挑选了0个好方法。 ... [详细] 如何解决《Spark:没有注册输出操作,因此无需执行任何操作》经验,为你挑选了1个好方法。 ... [详细] 如何解决《如何使用Gulp4async/await?》经验,为你挑选了1个好方法。 ... [详细] 如何解决《无法将'int(*)[size]'转换为'int**'》经验,为你挑选了1个好方法。 ... [详细] 如何解决《在Linux中使用DMA的最简单方法》经验,为你挑选了0个好方法。 ... [详细] 如何解决《来自Google地图的NullPointerException》经验,为你挑选了2个好方法。 ... [详细] 如何解决《hub.docker.com的自托管替代方案?》经验,为你挑选了1个好方法。 ... [详细] 如何解决《如何调试鱼脚本?》经验,为你挑选了1个好方法。 ... [详细] 如何解决《SwaggerUI/Editorlikepage-添加到我的网站》经验,为你挑选了1个好方法。 ... [详细] 如何解决《Laravel5.1@can,如何使用OR子句》经验,为你挑选了3个好方法。 ... [详细] 如何解决《我如何解决java2d中的ClassCastException(Bug-ID7172749)》经验,为你挑选了1个好方法。 ... [详细] 如何解决《如何在C中为数组分配数组》经验,为你挑选了1个好方法。 ... [详细] 如何解决《在numpy中索引多个非相邻范围》经验,为你挑选了1个好方法。 ... [详细] 如何解决《如果没有子集总和等于给定值,则返回最接近该值的子集总和》经验,为你挑选了1个好方法。 ... [详细] 如何解决《获取IntelliJ以识别AnnotationProcessor生成的类》经验,为你挑选了1个好方法。 ... [详细] 如何解决《如何从spring数据代码中清晰地(物理地)分离域层?》经验,为你挑选了1个好方法。 ... [详细]吐了个 "CAO" !Tags | 热门标签RankList | 热门文章
- 1Java JComboBox故障图形
- 2创建数据库连接昂贵吗?
- 3如何解析.txt文件?
- 4从MVC控制器使用Web API时返回JSON结果
- 5将内容推送到数组
- 6使用boto3完成multipart_upload?
- 7forEach回调不会使用getElementsByClassName执行
- 8Javascript正则表达式
- 9调整SwipeRefreshLayout的高度以将View放置在其底部
- 10我如何["字符串"]作为条件陈述?
- 11单击复选框时如何显示标记
- 12在jdbc连接中使用哪种设计模式?
- 13如何确定yocto中食谱的依赖性?
- 14自动完成参数powershell的值
- 15更改apache2服务器的ssl端口.(ERR_SSL_PROTOCOL_ERROR)
- 16Java无法读取代码的输出
- 17PyCharm当前的工作目录
- 18JS中的半结肠问题
- 19升级到angular2.0.0-beta.0后,router-link指令被破坏
- 20如何生成文件路径?
DevBox开发工具箱 | 专业的在线开发工具网站 京公网安备 11010802040832号 | 京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有