简体   繁体   中英

External javascript(jQuery) file could not be import in react js?

I have write a document ready event in my external script file. And i have include it in my index.html file using <script type="text/javascript" src="/path/to/script.js" But the events not triggered even the file path is correct.

If i put my script code in index.html using <script> //my code </script> the event working properly after browser loading the page. But if i redirect to another page using react-router the event not working.

I am using

React 16.3.2

JQuery 2.2.4

Thanks in advance

Actually using jquery with a react app is not a good idea but even so, did you try using " defer " attribute?

<script type="text/javascript" src="/path/to/script.js" defer>

and if you want to call your method on every client side page load you should implement something like this:

import { browserHistory } from 'react-router';

//Your initialization

browserHistory.listen( location =>  {
 //Do your stuff here
});

wrap all your codes with a function and call it in the listener.

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