I need to integrate sharethis in my reactjs application. I need that script to execute only in one component. Currently this is how I have implemented.
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);
}
The problem with this is since this component is mounted through react routing and page reload doesn't happen, the script doesn't re-execute.
I tried removing the script tag on componentWillUnmount lifecycle by using removeChild, but that still doesn't re-execute the script on mounting and I learnt that it happens because of this - The removed child node still exists in memory according to https://developer.mozilla.org/en-US/docs/Web/API/Node/removeChild.
Also, the script needs to re-execute on mount so that I get updated share count each time.
I work at sharethis, and we recently publish a reactjs plugin at https://www.npmjs.com/package/sharethis-reactjs. You can try to install and use it more conveniently.
Let me know if you have any issue while using it, and I'm happy to help.