I am currently working on a project using React with Webpacker on Rails 5. When I navigate to the page using the link_to
helper, it seems like the JavaScript under
document.addEventListener('turbolinks:load')
is not functioning properly.
The main controller for this project is called insurance_form_container.jsx
import React from 'react'
import ReactDOM from 'react-dom'
import StartForm from './insurance_form/start_form'
console.log('root react')
document.addEventListener('turbolinks:load',function () {
ReactDOM.render(
<StartForm />,
document.getElementById('start-form-index-container')
)
})
This is the HTML template for the specified route
<div class="container">
<div id="start-form-index-container">
</div>
</div>
<%= content_for(:footer) do %>
<%= javascript_pack_tag('insurance_form_container') %>
<% end %>
This is the link from another page.
<%= link_to users_insurance_form_start_path,
class: 'btn btn-lg btn-success btn-primary-green' do %>
Proceed
<% end %>
It appears that console.log('root react')
is executing correctly, but ReactDOM.render
is not running at all unless the page is refreshed. Am I overlooking something? How can I ensure that the `.jsx` code runs smoothly through the `link_to` helper?
Thank you!