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

5
  • I am facing the same problem did you get any solution ? Commented Apr 8, 2019 at 8: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'); } Commented Apr 8, 2019 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 Commented Apr 9, 2019 at 8:16
  • @AzadHussain can you find the fix.. i am facing the same issue Commented Jun 18, 2021 at 5:09
  • Refer to the below posted answer: stackoverflow.com/a/76165168/8101966 Commented May 3, 2023 at 15:20

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

1 Comment

window.jQuery = window.$ = $ did the trick for me
0

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

I think that is the problem here.

Comments

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

Comments

0

Try using import() inside componentDidMount()

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

import('your_URL')

4 Comments

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
looks like you are trying to import a JQuery minified version file. Use Jquery from NPM link
I am using jQuery from NPM for Ajax request.
So avoid importing jquery. try dynamically importing the rest of the files.
0

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

Comments

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)
  }, [])


Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.