如何使用添加的外部脚本来响应JS? [英] How do I use external script that I add to react JS?
本文介绍了如何使用添加的外部脚本来响应JS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的react组件中添加一个
I would like to add to my react component a
<script>http://xxx.xxx/XX.js</script>
我知道我可以简单地使用JSX添加它,但我不知道如何使用它,
I know I can simply add it using JSX , what I don't know is how to use it,
例如,此脚本具有一个称为A.Sort()的函数,如何调用它并从组件中使用它?
for instance this script has a function called A.Sort() , how can I call it and use it from a component?
推荐答案
您可以异步加载脚本并在加载时访问它.
You can load the script asynchronously and access it on load.
componentDidMount() {
const script = document.createElement("script");
script.src = "/static/libs/your_script.js";
script.async = true;
script.onload = () => this.scriptLoaded();
document.body.appendChild(script);
}
它应该附在window
上.
scriptLoaded() {
window.A.sort();
}
或
scriptLoaded() {
A.sort();
}
这篇关于如何使用添加的外部脚本来响应JS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文