0

I'm using Rails with webpacker with react.

I'm loading my components like this in my view file:

<div id="mycomponent"></div>
<%= javascript_pack_tag 'components/mycomponent' %>

In mycomponent, I have:

document.addEventListener('DOMContentLoaded', () => {
  ReactDOM.render(
    <MyComponent />,
    document.getElementById('mycomponent'),
  )
})

So on initial page load, mycomponent is mounted.
But when I visit another page, hit back or click on a link to the dashboard (in this case), the component doesn't load. Had to hit refresh to load it.

I think it has something to do with some caching techinques that Rails is using?
How do I get the components to load on every request

resting
  • 16,287
  • 16
  • 59
  • 90

1 Answers1

0

So turbolinks was doing some optimizations behind.

Realized that the url in chrome inspector remained the same when I changed pages.

Pages were loading fully after removing //= require turbolinks from app/assets/javascript/application.js.

Seen some people pointing to jquery.turbolinks for onload issues with turbolink. Not tested though.

resting
  • 16,287
  • 16
  • 59
  • 90