react-rails icon indicating copy to clipboard operation
react-rails copied to clipboard

Components don't re-render when changing pages with pjax

Open hampelm opened this issue 5 years ago • 0 comments

We use pjax on an Rails app we are slowly converting from older JS to React.

The issue: Components included withreact_component do render on the first page load, but don't render on pjax page changes. The docs suggest they should.

I'm not sure if this is just an issue with our config or with the way react-rails handles pjax interaction. I saw #742 but the comments don't seem to fix the issue I'm seeing.

I made a small test app to demonstrate this issue: https://github.com/hampelm/react-rails-pjax-debug

When you load the app, all components render: Screen Shot 2020-02-14 at 12 05 08 PM

When you browse to a new page and back, components in the area loaded by pjax do not render: Screen Shot 2020-02-14 at 12 05 15 PM

Files of note: javascripts/application.js initializes pjax Components are included in welcome/index.html.erb, welcome/two.html.erb, and application.html.erb.

Workaround I found: if I listen to pjax:end in javascripts/application.js and call ReactRailsUJS.mountComponents(), the app behaves as expected:

  $(document).on('pjax:end', function() {
    console.log('pjax:end');
    ReactRailsUJS.mountComponents() // This forces components to re-render
  })

System configuration

Sprockets or Webpacker version: 4.2.2 React-Rails version: 2.6.1 React_UJS version: not clear Rails version: 5.1.7

hampelm avatar Feb 14 '20 17:02 hampelm