2

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);
The Old County
  • 89
  • 13
  • 59
  • 129
  • `let $zoho = $zoho || {}` what do you expect this to do when there is no other `$zoho` variable in scope? (EDIT: and even if there were a `$zoho` variable in an outer scope, this would give the same error. In that case you simply need to change the variable's name.) – Robin Zigmond Feb 26 '21 at 17:52
  • -- that's just what was in the copy/paste code that they had? – The Old County Feb 26 '21 at 17:53
  • var $zoho=$zoho || {};$zoho.salesiq = $zoho.salesiq || {widgetcode:"xxxxxxxxxxxxxxxxxxxxxxxxxxx", values:{},ready:function(){}}; – The Old County Feb 26 '21 at 17:53
  • 1
    I don't understand where that code comes from, so who knows what it does. I certainly wouldn't simply lift it into a React component without understanding what it was doing and what I was doing that for! But anyway, presumably there is a global called `$zoho` loaded from some other script - but changing the `var` to a `let` leads to this error, because the JS engine thinks the `$zoho` on the right of the equals is the same local variable you're defining in the inner scope. Just change the name: eg `let $zoho2 = $zoho || {}`. – Robin Zigmond Feb 26 '21 at 17:56
  • I understand -- its a zoho live chat plugin - this code is in their website to launch some live chat thing --- I didn't see an npm module for it - but wondered if creating a react component to house it - then insert it would be the way to go – The Old County Feb 26 '21 at 18:03
  • OK thanks, I understand a bit better - but I'm really not sure why you need/want this in a React component? Why not just insert the code they provide - it's clearly what they expect you to do. In any case, if you insist on doing it, I hope I've explained how to avoid the error you're getting - it's basically a difference in the behaviour of `let` vs `var`. – Robin Zigmond Feb 26 '21 at 18:06
  • that didn't work your idea – The Old County Feb 26 '21 at 18:06
  • well its a really big site - components all over the place - they may want this active on only certain pages? – The Old County Feb 26 '21 at 18:07
  • 1
    I got it working -- with window. --- window.$zoho = window.$zoho || {}; window.$zoho.salesiq = window.$zoho.salesiq || {widgetcode:"xxxxxxxxx", values:{}, ready:function(){}}; – The Old County Feb 26 '21 at 18:08

0 Answers0