简体   繁体   English

React - 转换 HTML 脚本标签以异步加载 SDK

[英]React - Converting HTML script tag to load an SDK asynchronously

Currently I'm trying to add snapchat to a site through their SDK目前我正在尝试通过他们的 SDK 将 snapchat 添加到站点

my current attempt, can't figure out how to convert their Dom script into a function to be called when my component is loaded:我目前的尝试,无法弄清楚如何将他们的 Dom 脚本转换为 function 以在我的组件加载时调用:

export function snapchatSDK() {
  return new Promise(resolve => {
    const script = document.createElement('script');
    script.src = 'https://sdk.snapkit.com/js/v1/create.js';
    document.head.append(script);
  });

}

class Platforms extends React.Component {

  componentDidMount() {
    snapchatSDK();
  }

render() {
    return (
      <div>
      <p> Share on Social Media Platforms</p>
      <h4>Snapchat<h4>
       <button 
         className="snapchat-creative-kit-share"
         data-share-url= urlTobeShared()
          >
         Share me 
       </button>

       <h4>Twitter<h4>
       <button>
         Share me 
       </button>

      <h4>Reddit<h4>
       <button>
         Share me 
       </button>
      </div>
    );
  }
}

Here is a link to the doc's as well: snap doc这里也是文档的链接: snap doc

Your example doesn't make it very clear what you're trying to accomplish, but maybe one of these will help you figure it out?您的示例并不能很清楚地说明您要完成什么,但也许其中之一可以帮助您弄清楚?

Option 1选项1

Try the unoffical snapchat npm package .试试非官方的snapchat npm package (no idea if the package does what you need, buy maybe you haven't seen it yet?) (不知道 package 是否能满足您的需求,也许您还没看过?)

Option 2选项 2

Load the script in your HTML在 HTML 中加载脚本

<script src="https://sdk.snapkit.com/js/v1/create.js" />
<script src="/path/to/your/bundle.js" />

If loaded before your components mount, it should pick up your HTML and do whatever it does如果在您的组件安装之前加载,它应该拿起您的 HTML 并做任何事情

Option 3选项 3

Maybe try setting async = false on the script:也许尝试在脚本上设置async = false

const script = document.createElement('script');
script.src = 'https://sdk.snapkit.com/js/v1/create.js';
script.async = false
document.head.append(script);

Please see this article and this SO post .请参阅这篇文章和这篇SO 帖子 Key takeaway is:关键要点是:

Scripts that are dynamically created and added to the document are async by default动态创建并添加到文档中的脚本默认是异步的

I think that the main issue is how you are trying to load the script dynamically.我认为主要问题是您如何尝试动态加载脚本。 I am guessing that is what you want to do.我猜这就是你想要做的。 If so, there are several more steps.如果是这样,还有几个步骤。 Here is a link to a comparative example that goes through some of them: https://www.newline.co/fullstack-react/articles/Declaratively_loading_JS_libraries这是一个比较示例的链接,其中包含一些示例: https://www.newline.co/fullstack-react/articles/Declaratively_loading_JS_libraries

If you are using parcel2 or webpack4 (or create-react-app) then you could use dynamic import and most importantly without this issue如果您使用 parcel2 或 webpack4(或 create-react-app),那么您可以使用动态导入,最重要的是没有这个问题

However, the code is simple不过代码很简单

    import("https://sdk.snapkit.com/js/v1/create.js").then(_snap =>
      setSnap(_snap)
    );

if you are facing that issue you could use如果您遇到这个问题,您可以使用

    import(/* webpackIgnore: true */ "https://sdk.snapkit.com/js/v1/create.js").then(
      _snap => setSnap(_snap)
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM