Skip to content

Commit fa158e0

Browse files
author
William Meleyal
committed
set unmount event based on Turbolinks version
1 parent 0bbbb14 commit fa158e0

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ Component = React.createClass
8282

8383
`react_ujs` will also scan DOM elements and call `React.unmountComponentAtNode` on page unload. If you want to disable this behavior, remove `data-react-class` attribute in `componentDidMount`.
8484

85-
To use `react_ujs`, simply `require` it after `react` (and after `turbolinks` if [Turbolinks](https://github.com/rails/turbolinks) is used):
85+
To use `react_ujs`, simply `require` it after `react` (and after `turbolinks` if [Turbolinks](https://github.com/rails/turbolinks) is used).
8686

87-
**Note:** _`react_ujs` disables the Turbolinks cache. This is a workaround to ensure components are correctly unmounted. See [#87](https://github.com/reactjs/react-rails/issues/87) for details._
87+
**Note:** _Turbolinks >= 2.4.0 is recommended. For older versions `react_ujs` will disable the Turbolinks cache to ensure components are correctly unmounted. See [#87](https://github.com/reactjs/react-rails/issues/87) for details._
8888

8989
```js
9090
// app/assets/javascripts/application.js

lib/assets/javascripts/react_ujs.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@
3838

3939
var handleTurbolinksEvents = function() {
4040
var handleEvent;
41-
42-
// Disable the Turbolinks cache.
43-
Turbolinks.pagesCached(0);
41+
var unmountEvent;
4442

4543
if ($) {
4644
handleEvent = function(eventName, callback) {
@@ -51,8 +49,16 @@
5149
document.addEventListener(eventName, callback);
5250
}
5351
}
52+
53+
if (Turbolinks.EVENTS) {
54+
unmountEvent = Turbolinks.EVENTS.BEFORE_UNLOAD;
55+
} else {
56+
unmountEvent = 'page:receive';
57+
Turbolinks.pagesCached(0);
58+
}
59+
5460
handleEvent('page:change', mountReactComponents);
55-
handleEvent('page:receive', unmountReactComponents);
61+
handleEvent(unmountEvent, unmountReactComponents);
5662
};
5763

5864
var handleNativeEvents = function() {

0 commit comments

Comments
 (0)