Bundle external JavaScript(from a cdn) into a React component

前端 未结 4 485
北恋
北恋 2021-02-05 21:08

What options are there to bundle an external javascript sdk into a React Component?

I have tried including the javascript in the index.html and referring to it through

4条回答
  •  盖世英雄少女心
    2021-02-05 22:09

    To load external scripts from a CDN, a good option is to use the react-async-script-loader library. Not only can it load external JS files asynchronously, but it also loads on demand, i.e., lazy loading and supports both parallel and sequential loading.

    It allows you to decorate your component using an HOC like so:

    export default scriptLoader(
      [
        'https://cdnjs.cloudflare.com/somelibrary1.min.js',
        'https://cdnjs.cloudflare.com/somelibrary2.min.js'
      ]
    )(YourComponent)
    

提交回复
热议问题