If /page2 is loaded directly everything works as expected but when navigated to /page2 from /page1 the code in Utils.js is not executed and no errors are shown!
So for now, I figured out a small hack. By using forceReferesh={true} in BrowserRouter everything is working as expected. But I don’t to want reload the page for every new route.
Okay to summarize, if I include external js file on client/main.html and navigate to different routes, the functions inside that js file aren’t getting called. They are working fine, if I use forceRefresh={true} on BrowserRouter. Any help is appreciated, TIA.
The key thing to note being that the handlers are bound to the template, so they are added when the template is rendered and removed when it is destroyed. That way when your router renders a new page, and all the elements within it, the handlers are attached.
thanks for your answer. The function is just an example function ( i edited my post to make it more clear, my bad). The bigger problem is this: if I include external js file on main.html and navigate to different routes, the functions inside that js file aren’t getting called but works with the hack I mentioned.
Generally speaking JQuery and React don’t play nice together, especially in cases where you’re directly modifying the DOM. You’ll want to use React’s synthetic events instead of JQuery’s events.
@coagmano@copleykj guys…I can modify the code to react but I’m looking for the reason cause the same code is working if forceRefresh={true} on BrowserRouter is added.
Why the statement import '././..././js/somefile.js or inline script tag doesn’t make the functions work incase of routing but works upon refresh?
P.S: The js files on point are plugins/scripts file from a theme.