简体   繁体   中英

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);

}

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM