An Interest In:
Web News this Week
- April 2, 2024
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
January 11, 2022 11:07 am GMT
Original Link: https://dev.to/mulligan81/adding-external-javascript-script-to-react-3a25
Adding external javascript script to React
I'd like to include & run some js
file in the React
using Helmet
component. Here is the simple code:
index.js:
import React from "react";import ReactDOM from "react-dom";import { Helmet } from "react-helmet";import "./styles.css";function App() { console.log("op"); return ( <div className="App"> <Helmet> <script src="hello.js" type="text/jsx" /> </Helmet> <h1>Hellok CodeSandbox</h1> <h2>Start editing to see some magic happen!</h2> </div> );}
and ultra simple js
script to include & run:
hello.js:
console.log("opl882...")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:
<Helmet> <script type="text/javascript"> console.log("opl882..."); document.body.style.backgroundColor = "red" </script> </Helmet>
it works!
Why doesn't the external js
file work?
Original Link: https://dev.to/mulligan81/adding-external-javascript-script-to-react-3a25
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To