pgl*_*z82 2 javascript reactjs
我正在尝试创建一个组件,该组件在我的 React 单页应用程序的某些路由中呈现Buy Me A Coffee小部件。到目前为止,这是我的组件:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
当我使用开发人员工具检查页面时,标签位于头部部分的末尾,一切似乎都正确,但小部件未显示在页面中。
注意:如果我在 index.html 页面的 head 部分复制脚本代码:
<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>
Run Code Online (Sandbox Code Playgroud)
然后它完美地工作。问题是,在这种情况下,所有路线都会显示小部件。我也尝试过 react-helmet 没有成功。
I finnally got it working. I post my solution here in case it is useful for someone. Not sure if it is the most elegant but it works. I slightly modified the original component so the script and widget is removed when the component is unmounted. Here is the component now:
class BuyMeACoffe extends React.Component {
constructor(props){
super(props)
let 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
//Call window on load to show the image
script.onload=function(){
var evt = document.createEvent('Event');
evt.initEvent('DOMContentLoaded', false, false);
window.dispatchEvent(evt);
}
this.script=script
}
componentDidMount () {
document.head.appendChild(this.script)
}
componentWillUnmount(){
document.head.removeChild(this.script);
document.body.removeChild(document.getElementById("bmc-wbtn"))
}
render(){
return(null)
}
}
Run Code Online (Sandbox Code Playgroud)
Basically, after reading the buymeacoffee script I realized that they load the widget when the window is DOMContentLoaded event is fired. What I do is to fire this event manually after I insert the script.
Note: After doing this, I had to add a couple of styles because the widget was showing out of the screen. Maybe it was because of my layout. Anyway, here they are in case they you need them:
#bmc-wbtn{
bottom: 15px;
}
#bmc-wbtn + div{
bottom:15px;
}
Run Code Online (Sandbox Code Playgroud)