0 Replies - 311 Views - Last Post: 11 January 2022 - 04:01 AM

#1 Derek911   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-August 20

Adding external javascript script to React.

Posted 11 January 2022 - 04:01 AM

I'd like to include & run some `js` file in the `React` using `Helmet` component. Here is the simple code:


 import React from "react";
    import ReactDOM from "react-dom";
    import { Helmet } from "react-helmet";
    import "./styles.css";
    function App() {
      return (
        <div className="App">
            <script src="hello.js" type="text/jsx" />
          <h1>Hellok CodeSandbox</h1>
          <h2>Start editing to see some magic happen!</h2>

and ultra simple `js` script to include & run:


    document.body.style.backgroundColor = "red";

But the script seems NOT to work! - i have no console output and/or background color changed. What's odd when I use the `js` code as an inline code like:

       <script type="text/javascript">
         console.log("opl882..."); document.body.style.backgroundColor = "red"

it works!

Why doesn't the external `js` file work?

Is This A Good Question/Topic? 0
  • +

Page 1 of 1