I am trying to add a bit js from zoho chat into its own react module.
<script type="text/javascript">var $zoho=$zoho || {};$zoho.salesiq = $zoho.salesiq || {widgetcode:"xxxxxxxxxxxxxxxxxxxxxxxxxxx", values:{},ready:function(){}};var d=document;s=d.createElement("script");s.type="text/javascript";s.id="zsiqscript";s.defer=true;s.src="https://salesiq.zoho.eu/widget";t=d.getElementsByTagName("script")[0];t.parentNode.insertBefore(s,t);d.write("<div id='zsiqwidget'></div>");</script>
I've built this component but I get a reference error -- "ReferenceError: Cannot access '$zoho' before initialization"
import React, { Component } from 'react';
import { withRouter } from 'react-router-dom';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import Button from '@material-ui/core/Button';
import parse from 'html-react-parser';
import './ZohoLiveChat.scss';
class ZohoLiveChat extends Component {
componentDidMount () {
let $zoho = $zoho || {};
$zoho.salesiq = $zoho.salesiq || {widgetcode:"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", values:{}, ready:function(){}};
let d= document;
let s = d.createElement("script");
s.type ="text/javascript";
s.id ="zsiqscript";
s.defer = true;
s.src = "https://salesiq.zoho.eu/widget";
let t = d.getElementsByTagName("script")[0];
t.parentNode.insertBefore(s,t);
d.write("<div id='zsiqwidget'></div>");
}
render() {
return (
<div className="common-block">
ZOHO!!!!!!!!!!!!!
</div>
)
}
}
function mapStateToProps(state) {
return {
};
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({ }, dispatch);
}
export default withRouter(connect(mapStateToProps, mapDispatchToProps)(ZohoLiveChat));
I've seen this example - but not sure its the way to implement
Adding script tag to React/JSX
const script = document.createElement("script");
script.src = "https://use.typekit.net/foobar.js";
script.async = true;
document.body.appendChild(script);