I am trying to create a component that renders the Buy Me A Coffee widget in some routes of my react single page application. This is my component so far:
class BuyMeACoffee extends React.Component {
componentDidMount () {
const script = document.createElement("script",);
script.setAttribute('data-name','BMC-Widget')
script.src = "https://cdnjs.buymeacoffee.com/1.0.0/widget.prod.min.js"
script.setAttribute('data-id', 'boulderproblems');
script.setAttribute('data-description', 'Thank you for your support!');
script.setAttribute('data-message', 'This web is free to use. Do you want to help supporting it?');
script.setAttribute('data-color',"#FF5F5F")
script.setAttribute('data-position','right')
script.setAttribute('data-x_margin','18')
script.setAttribute('data-y-margin','18')
script.async = true
document.head.appendChild(script)
}
render(){
return(null)
}
}
When I inspect the page with the developer tools, the tag is at the end of the head section and everything seems correct but the widget doesn't show in the page.
Note: If I copy the script code in the head section of the index.html page:
<script data-name="BMC-Widget" src="https://cdnjs.buymeacoffee.com/1.0.0/widget.prod.min.js" data-id="boulderproblems" data-description="Thank you for your support!" data-message="This web is free to use. Do you want to help supporting it?" data-color="#FF5F5F" data-position="right" data-x_margin="18" data-y-margin="18"></script>
then it works perfectly. The problem is that in this case, the widget is shown for all routes. I have also tried react-helmet without success.

