'How to add external js file in react application?
I have created one js file in our application. in this js file How add in our page?
I had Written below code for our application. First of I had written online link that's work perfect. then after download js and set path to js file. but not proper working.
componentDidMount() {
const script = document.createElement("script");
// script.src = "https://code.jquery.com/jquery-1.12.4.min.js";
script.src = "../../Content/JS/jquery-1.12.4.min.js";
script.async = true;
document.body.appendChild(script);
}
Solution 1:[1]
You need to install and then import jQuery or any other plugin you need in your component.
npm install {package name}
in your package JSON.
import jquery from 'jquery'
in your component
Here is an example
Solution 2:[2]
If you want to add additional js file (script). This is the easiest way to to do this.
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
<script src="../../Content/JS/jquery-1.12.4.min.js"></script>
</body>
This is the root html file of your react application. According to your folder structure path can be changed.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sathya P |
| Solution 2 | Dushan Ranasinghe |
