question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Components not cleaned up with turbo links navigation

See original GitHub issue

Steps to reproduce

Navigate with turbolinks to pages with render_component.

Expected behavior

Components should be unmounted when navigating to different pages.

Actual behavior

Each page visit renders a new component while leaving the previous ones in place. With turbolinks cached pages subsequent page visits renders 2 new components from what I assume is the cached page, then the new page content.

System configuration

Sprockets or Webpacker version: webpacker (4.0.7) React-Rails version: react-rails (2.6.0) Rect_UJS version: react_ujs@^2.6.0 Rails version: 6.0 Ruby version: 2.6.3


It looks like a fix for this might have been submitted with PR #1020. That is almost a month old so maybe its a different issue?

Adding the following code to application.js seems to work as a temporary work around.

document.addEventListener('turbolinks:before-render', () => {
    ReactRailsUJS.handleUnmount()
});

It appears the default way ReactRailsUjs is handling unmounting with turbolinks is the issue.

Sample app that recreates the problem. https://github.com/mmccall10/turbolinks-react-rails-demo

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
jkbaileycommented, Sep 28, 2019

Thanks for mentioning my PR, also thanks for the quick fix for now. Not sure how to speed up the PR though.

1reaction
jdelStrothercommented, Jun 6, 2022

So now that that PR’s been merged, what’s the expected way of cleaning up react components on leaving the page?

Some of our components use setInterval to do something every x seconds, which call clearInterval in componentWillUnmount. Others might load and/or decode a large file, which gets aborted in componentWillUnmount.

I could possibly clean these up by having each component listen for turbolinks cleanup events, but having those components have to know that they’re living in a turbolinks+react_ujs stack seems like unnecessary coupling.

The PR talks about fixing the scroll-position restoration, but that seems like something that should have been fixed upstream in Turbolinks rather than removing component-unmounts from ReactUJS.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bootstrap Native does not work with Turbo Links
When I first load the page, the Bootstrap drop down menu works fine, but after navigating to another page, the Bootstrap drop down...
Read more >
Opting Out of Caching - Turbo Handbook
Permanent elements persist across page loads, so that any changes you make to those elements do not need to be reapplied after navigation....
Read more >
How We Migrated To Turbolinks Without Breaking Javascript
Don't Expect a Clean DOM ... Turbolinks caches pages right before you navigate away from them. That's probably after your JavaScript has ...
Read more >
Turbolinks Cheatsheet - Key Shift in Cmd
In unsupported browsers, Turbolinks gracefully degrades to standard navigation. Installation. <script> tag or a traditional concatenated ...
Read more >
Turbolinks - The react way - Development - TheForeman
remounting vertical navigation and topbar component on every transition. All of the above cause a performance drawback, plus pages load without ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found