How to refresh Addthis in react?

你离开我真会死。 提交于 2021-01-29 17:10:11

问题


import React from "react";
import LoadScript from "react-load-script";
function Addthis() {
  return (
    <LoadScript url="http://s7.addthis.com/js/300/addthis_widget.js#pubid=ra-xxxx" />
  );
}

export default Addthis;

This is the code that I have so far to initialize addthis. I found this issue on GitHub, but no solutions yet: https://gist.github.com/jonathanconway/9925286


回答1:


Use something like this:

import React from "react";
import ReactDOM from "react-dom";
import LoadScript from "react-load-script";
import "./styles.css";        

function Addthis() {
  const handleScriptLoad=()=>{    
    window.addthis.init();
    window.addthis.toolbox('.addthis_toolbox')    
    console.log("addthis Loaded");
  }
  return (
    <div>
    <LoadScript 
     url="https://s7.addthis.com/js/300/addthis_widget.js#pubid=ra-xxxx" 
    onLoad={handleScriptLoad}
    />    
    <div class="addthis_toolbox addthis_default_style " style={{margin: "20px"}}>
    <a class="addthis_button_preferred_1"></a>
    <a class="addthis_button_preferred_2"></a>
    <a class="addthis_button_preferred_3"></a>
    <a class="addthis_button_preferred_4"></a>
    <a class="addthis_button_compact"></a>
    <a class="addthis_counter addthis_bubble_style"></a>
</div>
<hr/>
</div>
  );
}


function App() {  
  return (
    <div className="App">
      <Addthis />
      <h1>Hello addthis</h1>
      <h2>Buttons loaded (upper, left) of screen</h2>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

see answer output : HERE



来源:https://stackoverflow.com/questions/59574000/how-to-refresh-addthis-in-react

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!