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

How to not unmount components on page navigations?

Open 4ndv opened this issue 6 years ago • 5 comments

I have some components, that should not be unmounted during their "presence" on the page, but should be unmounted if next page doesn't have them.

How it can be done?

4ndv avatar Mar 20 '19 09:03 4ndv

Setting id and data-turbolinks-permanent does not help:

<%= react_component("ActiveTimer", {}, class: 'active-timer-menu', 'data-turbolinks-permanent': true, id: 'active-timer-menu') %>

https://github.com/turbolinks/turbolinks/blob/master/README.md#persisting-elements-across-page-loads

4ndv avatar Mar 20 '19 09:03 4ndv

Looks like #962 is related to this

4ndv avatar Mar 20 '19 10:03 4ndv

Yep. Looks related to the linked PR.

BookOfGreg avatar Mar 20 '19 14:03 BookOfGreg

Ugly, but seemingly working temporary workaround:

Add this to your component (inspired by solution in #119, but modified for modern react):

import ReactDOM from 'react-dom'

componentDidMount () {
  ReactDOM.findDOMNode(this).parentElement.removeAttribute('data-react-class')
}

Use react_component like this (not a temporary, this way it SHOULD work, but doesn't without lines above):

<%= react_component("ComponentName", {}, 'data-turbolinks-permanent': true, id: 'unique-id-for-turbolinks') %>

4ndv avatar Mar 20 '19 16:03 4ndv

EDIT: I apologize, I should have read the docs - the javascript_pack_tag needs to come after turbolinks in the head. When I do that, it works perfectly fine for me.

gregblass avatar May 22 '19 18:05 gregblass

Closing the issue as we have a solution.

alkesh26 avatar Nov 04 '22 06:11 alkesh26