I've tried a few different things, but I'm unable to get my React components to render when navigating using the browser back button (sometimes even the forward button).
It doesn't seem to matter whether I prerender them server-side or if I render them client-side. E.g., this:
<%= react_component('TopicLinker', {topicName: @topic.name, topicId: @topic.slug}, {prerender: true}) %>
The entire mount node just doesn't show up in the DOM when navigating using the browser back button - multiple browsers, too - Safari (latest), Chrome (latest), etc - so I don't think this is an issue with React.js itself - feels more like it has to do with react-rails.
Thoughts? I know this is vague, but I'm really not sure what I'm doing wrong (only thing I can seem to think is that Turbolinks might be breaking this somehow). Everything else with react-rails works perfect, except with browser back navigation.
Did some more testing, and it looks like it seems to come down to Turbolinks. If I disable Turbolinks (e.g. data-no-turbolink on the body element in my layouts), the problem sort of goes away - at least for Chrome. But on Safari, it becomes even more unpredictable.
I'd prefer to not disable Turbolinks... but I'm worried that this is going to be a limitation of react-rails.
Looks like this resolves this for Safari - it's combination of disabling Turbolinks, and the Safari window.onunload trick:
<body data-no-turbolink data-no-transition-cache>
...
<script type="text/javascript">
window.onunload = function(){};
</script>
</body>
Yes, I think some versions of react-rails + some versions of Turbolinks have this bug:
I think the solutions are:
Turbolinks.pagesCached(0)master + the latest version of TurbolinksSee related #89
I had similar issues and my solution was to remove the data-react-class attribute in the component rendered by react_component. Something along these lines inside the component:
componentDidMount: function() {
$(this.getDOMNode()).parent().removeAttr('data-react-class');
},
The root of the issue seemed to be Turbolinks saving page state when leaving the page and after component unmounting in rails_ujs. I'm not sure what kind of long-term issues this might cause but seems to work so far.
The problem is with this line https://github.com/reactjs/react-rails/blob/master/lib/assets/javascripts/react_ujs.js.erb#L51 which when unmounting the React component also deletes the root node, so when coming back there is nothing on the page to mount again. I'm not sure why its doing that but if I remove that line things work for me.
Wow, I didn't realize turbolinks cached the actual _document_ before changing pages. I always assumed it cached the server's response for the URL.
Turbolinks caching the document as-is
which happens before fetching a new page
Le sigh, better sort that out

the document.body is the actual content, please delete https://github.com/reactjs/react-rails/blob/master/lib/assets/javascripts/react_ujs.js.erb#L51
Taking a pass at it here: #190, can someone checkout that commit and confirm it solves the problem?
I've merged #190, please update to master and reopen if you experience this issue again. Thanks for reporting it!
Is this back? I'm seeing something that might be related.. when I click on an anchor link my component gets removed.
Most helpful comment
Is this back? I'm seeing something that might be related.. when I click on an anchor link my component gets removed.