装入所有项目后插入外部脚本

时间:2019-01-19 10:39:50

标签: javascript reactjs

我正在尝试将一个外部脚本附加到我的项目,该脚本将元素上的click事件绑定。但是,通过我的尝试,它会将脚本和所有已安装的项目添加到文档中。

所以在components/shop/Shop.js

componentDidUpdate() {
    const script = document.createElement('script');

    script.src = 'https://embed.selly.gg';
    script.async = true;

    document.body.appendChild(script);
    console.log('updated');
}

我将其附加到components/shop/ShopItem.js

<button className={classes.button} data-selly-product-{product.id}>
    Buy
</button>

因此,我只想附加一次脚本,但是将所有项目与脚本包含的事件绑定在一起。我该怎么做?

0 个答案:

没有答案