javascript 将外部脚本导入 React 组件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46142918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 06:29:26  来源:igfitidea点击:

Import external script into React component

javascriptjqueryreactjs

提问by Mervyn Lee

I am converting a template into react components for my site. However, the template are mostly in jquery. The external javascripts are not binding well with my component and I have to bind the function with componentDidMount().

我正在将模板转换为我网站的 React 组件。但是,模板主要是在 jquery 中。外部 javascripts 无法与我的组件很好地绑定,我必须将函数与componentDidMount().

componentDidMount(){
  $(".b-upper-title").slabText({
      // Don't slabtext the headers if the viewport is under 380px
      "viewportBreakpoint":380
  });
}

However, slabText()is not working since it is dependent on <script src="styles/plugins/slabText/js/jquery.slabtext.min.js"></script>. I need to import this script into the component in order to call the function. Any idea how to perform the task?

但是,slabText()由于它依赖于<script src="styles/plugins/slabText/js/jquery.slabtext.min.js"></script>. 我需要将此脚本导入到组件中才能调用该函数。知道如何执行任务吗?

采纳答案by Max Millington

Try taking a look at this library react-async-script-loader

尝试看看这个库react-async-script-loader