2

I am creating a react app using create-react-app. I have some external javascript libraries for template design in main index.html

 <script src="%PUBLIC_URL%/js/jquery-2.2.4.min.js"></script>
 <script src="%PUBLIC_URL%/js/common_scripts.js"></script>
 <script src="%PUBLIC_URL%/js/main.js"></script>
 <script src="%PUBLIC_URL%/js/owl.carousel.js"></script>

These JS files are working on first page when the application gets started. But these libraries are not working when redirect to other page from first page. From what I understand, these files are rendering but their functions, variables, and methods are not accessible when the route is changed. I am using react-router-dom v4 for routing .

I googled it and found a solution- To render the JS libraries by ComponentDidMount() method

ComponentDidMount(){
    loadjs('./js/main.js', function(){
    loadjs('./js/common_scripts.js)
    });
  }

But even this solution is not working. Please help to solve this issue.

Thanks

Saurabh Sharma
  • 804
  • 6
  • 16
  • 42
  • I am facing the same problem did you get any solution ? – Azad Hussain Apr 08 '19 at 08:49
  • 1
    I used `window` object to use jquery functions and events `import $ from 'jquery';` in main `index.js` and then access any jquery events like `componentDidUpdate(nextProps, nextState) { window.$('.hero_single').addClass('start_bg_zoom'); }` – Saurabh Sharma Apr 08 '19 at 13:39
  • 1
    thank you for the reply but I am new to front end development so I didn't get what you said, I have multiple external js file like in your case like jquery, bootstrap, owl.carousel, nice select, and a custom main.js which includes all initialization etc How can I make all page work fine – Azad Hussain Apr 09 '19 at 08:16
  • @AzadHussain can you find the fix.. i am facing the same issue – Mitesh K Jun 18 '21 at 05:09
  • Refer to the below posted answer: https://stackoverflow.com/a/76165168/8101966 – Jules Ntare May 03 '23 at 15:20

6 Answers6

1

This is how i import Jquery into my create react app

Jquery:

  1. first run npm install jquery
  2. index.js

import * as $ from 'jquery'

window.jQuery = window.$ = $

see: http://blog.oddicles.org/create-react-app-importing-bootstrap-jquery-cleanly-node-js/

Alternativly you can attach the scripts to a window object and then call them as needed:

Try following steps:

including the external js file link in your /public/index.html use the external library with prefix window. for example, JQuery.

put following line in your /public/index.html

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>`

use it in your project:

window.$("#btn1").click(function(){
    alert("Text: " + $("#test").text());
});

`

See this for more details:

https://github.com/facebook/create-react-app/issues/3007

Bot
  • 134
  • 6
0

You are using componentWillMount() instead of componentDidMount().

I think that is the problem here.

nikamanish
  • 662
  • 4
  • 17
0

You can set that js files on window object and then you can access it by window.your_name to object..

You have to set that in index file

Bhavin Padiya
  • 87
  • 2
  • 14
0

Try using import() inside componentDidMount()

The import() is self explaining.It just import's the JavaScript files.

import('your_URL')
Bharath
  • 335
  • 2
  • 8
  • 1
    Hi, I tried but its giving error of vriables like `Line 3: 'WOW' is not defined no-undef Line 3: 'Switchery' is not defined no-undef Line 3: Unexpected use of 'location' no-restricted-globals Line 3: Unexpected use of 'location' no-restricted-globals` – Saurabh Sharma Sep 07 '18 at 09:36
  • looks like you are trying to import a JQuery minified version file. Use Jquery from NPM [link](https://www.npmjs.com/package/jquery) – Bharath Sep 07 '18 at 10:07
  • I am using jQuery from NPM for Ajax request. – Saurabh Sharma Sep 07 '18 at 10:49
  • So avoid importing jquery. try dynamically importing the rest of the files. – Bharath Sep 07 '18 at 12:45
0

Try to call event using window object. reference <a href="https://github.com/facebook/create-react-app/issues/3007#issuecomment-357863643">Link</a>

Anjaney Mishra
  • 141
  • 1
  • 2
  • 13
0
const loadScript = (src) => {
  return new Promise(function (resolve, reject) {
    var script = document.createElement('script')
    script.src = src
    script.addEventListener('load', function () {
      resolve()
    })
    script.addEventListener('error', function (e) {
      reject(e)
    })
    document.body.appendChild(script)
    document.body.removeChild(script)
  })
}

 useEffect(() => {
    loadScript(`${process.env.PUBLIC_URL}/js/plugin.min.js`)
    setTimeout(() => {
      setTimeout(() => {
        setLoading(false)
      }, 500)
      loadScript(`${process.env.PUBLIC_URL}/js/main.js`)
    }, 200)
  }, [])


Mitesh K
  • 694
  • 1
  • 11
  • 24