首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ReactJS中集成sharethis

在ReactJS中集成sharethis
EN

Stack Overflow用户
提问于 2018-08-07 12:42:52
回答 2查看 1.7K关注 0票数 1

我需要将sharethis集成到我的reactjs应用程序中。我只需要在一个组件中执行该脚本。目前,我就是这样实施的。

代码语言:javascript
复制
 componentWillMount() {
 const script = document.createElement("script");
 script.src ="//platform-api.sharethis.com/js/sharethis.js#property=242434664&product=sop?r=" +new Date().getTime();
 script.async = true;
 script.id = "shareThisId";
 document.body.appendChild(script);
}

这方面的问题在于,因为这个组件是通过反应路由挂载的,并且页面重新加载不会发生,所以脚本不会重新执行。

我尝试使用componentWillUnmount生命周期中的removeChild删除脚本标记,但在安装时仍然没有重新执行脚本,而且我了解到正是由于这个原因--根据https://developer.mozilla.org/en-US/docs/Web/API/Node/removeChild,删除的子节点仍然存在于内存中。

另外,脚本需要在挂载上重新执行,以便每次都得到更新的共享计数。

EN

回答 2

Stack Overflow用户

发布于 2018-09-12 19:29:03

我在sharethis工作,最近我们在https://www.npmjs.com/package/sharethis-reactjs上发布了一个reactjs插件。您可以尝试安装和使用它更方便。

如果你在使用时有任何问题,请告诉我,我很乐意帮忙。

票数 3
EN

Stack Overflow用户

发布于 2018-10-12 00:58:13

好许,不管它有什么价值,我厌倦了让“官方”分享

您需要在index.html中包含js

代码语言:javascript
复制
<script type='text/javascript' src='//platform-api.sharethis.com/js/sharethis.js#property=YOUR-PROPPERTY-KEY&product=inline-share-buttons' async='async'></script>

这就是我写的..。当然,你会想用你的方式来配置它

代码语言:javascript
复制
import React, {Component} from 'react'
import {isMobile} from 'react-device-detect';


export class ShareButtons extends Component{
    constructor(props){
        super(props);

        var defaultNetworks = isMobile  ? ['facebook', 'twitter', 'pinterest', 'googleplus', 'sharethis', 'sms', 'email'] 
                                        : ['facebook', 'twitter', 'pinterest', 'googleplus', 'sharethis',  'email'];

    this.state = {
        config: {
            id: this.props.id || 'sharethis-inline-buttons',
            networks: this.props.networks || defaultNetworks,
            alignment: this.props.alignment || "right",
            enabled: this.props.enabled !== 'false' || true,
            font_size: this.props.font_size || 11,
            padding: this.props.padding || 8,
            radius: this.props.radius || 20,
            networks: this.props.networks || defaultNetworks,
            size: this.props.size || 32,
            show_mobile_buttons: this.props.show_mobile_buttons !== 'false' || true,
            spacing: this.props.spacing || 2,
            url: this.props.url || window.location.href,
            title: this.props.title || document.title,
            image: this.props.image || "https://18955-presscdn-pagely.netdna-ssl.com/wp-content/uploads/2016/12/ShareThisLogo2x.png", // useful for pinterest sharing buttons
            description: this.props.description || document.getElementsByTagName('meta')["description"].content,
            username: this.props.username || "ChadSteele" // custom @username for twitter sharing
        }
    };



    }

    componentDidMount(){
        window.__sharethis__.load('inline-share-buttons', this.state.config);
    }

    render(){
        return <span id={this.state.config.id || 'sharethis-inline-buttons'}></span>
    }
}

export default ShareButtons;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51727108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档