admin管理员组文章数量:1390812
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/mon_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/mon_scripts.js)
});
}
But even this solution is not working. Please help to solve this issue.
Thanks
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/mon_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/mon_scripts.js)
});
}
But even this solution is not working. Please help to solve this issue.
Thanks
Share Improve this question edited Sep 6, 2018 at 9:14 Saurabh Sharma asked Sep 6, 2018 at 9:03 Saurabh SharmaSaurabh Sharma 8047 gold badges16 silver badges42 bronze badges 5- I am facing the same problem did you get any solution ? – Azad Hussain Commented Apr 8, 2019 at 8:49
-
1
I used
window
object to use jquery functions and eventsimport $ from 'jquery';
in mainindex.js
and then access any jquery events likeponentDidUpdate(nextProps, nextState) { window.$('.hero_single').addClass('start_bg_zoom'); }
– Saurabh Sharma 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 – Azad Hussain Commented Apr 9, 2019 at 8:16
- @AzadHussain can you find the fix.. i am facing the same issue – Mitesh K Commented Jun 18, 2021 at 5:09
- Refer to the below posted answer: stackoverflow./a/76165168/8101966 – Jules Ntare Commented May 3, 2023 at 15:20
6 Answers
Reset to default 1This is how i import Jquery into my create react app
Jquery:
- first run
npm install jquery
index.js
import * as $ from 'jquery'
window.jQuery = window.$ = $
see: http://blog.oddicles/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./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./facebook/create-react-app/issues/3007
You are using ponentWillMount()
instead of ponentDidMount()
.
I think that is the problem here.
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
Try using import()
inside ponentDidMount()
The import() is self explaining.It just import's the JavaScript files.
import('your_URL')
Try to call event using window object. reference <a href="https://github./facebook/create-react-app/issues/3007#issuement-357863643">Link</a>
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)
}, [])
本文标签: reactjsExternal Javascript is not working in reactjsStack Overflow
版权声明:本文标题:reactjs - External Javascript is not working in react.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744684271a2619602.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论