【发布时间】:2018-06-14 02:52:18
【问题描述】:
我在 react JSX 中加载外部脚本时遇到问题
<script>
(function(d) {
var config = {
kitId: 1234567,
scriptTimeout: 3000,
async: true
},
h=d.documentElement,t=setTimeout(function(){h.className=h.className.replace(/\bwf-loading\b/g,"")+" wf-inactive";},config.scriptTimeout),tk=d.createElement("script"),f=false,s=d.getElementsByTagName("script")[0],a;h.className+=" wf-loading";tk.src='https://use.typekit.net/'+config.kitId+'.js';tk.async=true;tk.onload=tk.onreadystatechange=function(){a=this.readyState;if(f||a&&a!="complete"&&a!="loaded")return;f=true;clearTimeout(t);try{Typekit.load(config)}catch(e){}};s.parentNode.insertBefore(tk,s)
})(document);
</script>
这是我的渲染函数,我想让 javascipt 异步加载,它在 html 文件中非常容易,但是我对如何实现的反应组件感到震惊。 (如果我可以避免安装另一个外部模块最好)。非常感谢
render() {
return (
<head>
//Script to load asyn
</head>
)
}
【问题讨论】:
-
您尝试执行的操作不起作用。而是在
componentDidMount()stackoverflow.com/questions/13121948/… 中尝试以下策略之一 -
在你的 html 中注入的 react 组件在哪里?
-
是的,我不清楚您要做什么。你能提供一个你的实现不起作用的小提琴吗?
标签: reactjs