DarkianZ's avatar

Script doesn't load with react-script-tag

Hello everybody, after solving a problem with the node modules and babel/runtime, i finally can see my script on the console, attached to the html, but it looks like he is "display:none", i mean he is in grey color, like if he was ignored. I get an error on the console, i will show you right now which one is it. If anyone had this problem or know how to solve it...

Uncaught SyntaxError: expected expression, got '<'

The script from “http://localhost:3000/scripts.js” was loaded even though its MIME type (“text/html”) is not a valid JavaScript MIME type.

This is the errors i get, when trying with useEffect and react-script-tag.



    useEffect(() => {
        const script = document.createElement('script');

        script.src = "scripts.js";
        script.async = true;

        document.body.appendChild(script);

        return () => {
            document.body.removeChild(script);
        }
    }, []);

   <ScriptTag isHydrating={true} type="text/javascript" src="script.js" />

0 likes
5 replies
Sinnbeck's avatar

Can you show the complete component? It's hard to help debug based on just two tiny parts of it

DarkianZ's avatar

@Sinnbeck sure, sorry..

this was in app.js

import ScriptTag from 'react-script-tag';


function App() {


  return (

    <>
      <ScriptTag isHydrating={true} type="text/javascript" src="script.js" />

      <Switch>


        <Route exact path="/">
          <Homepage />
        </Route>

        <Route exact path="/login">
          <Login />
        </Route>


        <Adminprivate />

      </Switch>

    </>



  );
}

export default App;

and this my homepage

const Homepage = () => {



    useEffect(() => {
        const script = document.createElement('script');

        script.src = "scripts.js";
        script.async = true;

        document.body.appendChild(script);

        return () => {
            document.body.removeChild(script);
        }
    }, []);



    return (
        <>

            <section className="s1">


                <img className="logo-geston" async
                    src={Geston}
                    alt="" />
                <h2 className="menu">Menu</h2>

                <input id="toggle" type="checkbox"></input>
                <label htmlFor="toggle" id="burg" className="hamburger">
                    <div className="top-bun"></div>
                    <div className="meat"></div>
                    <div className="bottom-bun"></div>
                </label>
</section>
</>
)}

this is the script

  var x, i, j, l, ll, selElmnt, a, b, c;
  
  x = document.getElementsByClassName("custom-select");
  l = x.length;
  for (i = 0; i < l; i++) {
    selElmnt = x[i].getElementsByTagName("select")[0];
    ll = selElmnt.length;
  
    a = document.createElement("DIV");
    a.setAttribute("class", "select-selected");
    a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
    x[i].appendChild(a);
    b = document.createElement("DIV");
    b.setAttribute("class", "select-items select-hide");
    for (j = 1; j < ll; j++) {
    
      c = document.createElement("DIV");
      c.innerHTML = selElmnt.options[j].innerHTML;
      c.addEventListener("click", function (e) {
  
        var y, i, k, s, h, sl, yl;
        s = this.parentNode.parentNode.getElementsByTagName("select")[0];
        sl = s.length;
        h = this.parentNode.previousSibling;
        for (i = 0; i < sl; i++) {
          if (s.options[i].innerHTML == this.innerHTML) {
            s.selectedIndex = i;
            h.innerHTML = this.innerHTML;
            y = this.parentNode.getElementsByClassName("same-as-selected");
            yl = y.length;
            for (k = 0; k < yl; k++) {
              y[k].removeAttribute("class");
            }
            this.setAttribute("class", "same-as-selected");
            break;
          }
        }
        h.click();
      });
      b.appendChild(c);
    }
    x[i].appendChild(b);
    a.addEventListener("click", function (e) {
   
      e.stopPropagation();
      closeAllSelect(this);
      this.nextSibling.classList.toggle("select-hide");
      this.classList.toggle("select-arrow-active");
    });
  }
  
  function closeAllSelect(elmnt) {
  
    var x, y, i, xl, yl, arrNo = [];
    x = document.getElementsByClassName("select-items");
    y = document.getElementsByClassName("select-selected");
    xl = x.length;
    yl = y.length;
    for (i = 0; i < yl; i++) {
      if (elmnt == y[i]) {
        arrNo.push(i)
      } else {
        y[i].classList.remove("select-arrow-active");
      }
    }
    for (i = 0; i < xl; i++) {
      if (arrNo.indexOf(i)) {
        x[i].classList.add("select-hide");
      }
    }
  }
  
  document.addEventListener("click", closeAllSelect); 

i've tried both ways

Please or to participate in this conversation.