如何使用我添加的外部脚本来响应 JS? [英] How do I use external script that I add to react JS?
本文介绍了如何使用我添加的外部脚本来响应 JS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想添加到我的反应组件中
I would like to add to my react component a
我知道我可以简单地使用 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屋!
查看全文