-
Notifications
You must be signed in to change notification settings - Fork 759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compatibility with Turbo (next version of Turbolinks) #1103
Comments
Another thing to consider is turbo-frame events once that is merged At the moment React components that come in via a frame do not mount even with the above hotfix. And as per the above PR there is currently no existing events to tap into. |
@phoozle They've added |
Has anyone gotten this to work recently? I have the latest version (v7.0.1) installed locally and am still unable to render |
@hbriggs Unfortunately no. Personally, I’ve started using Stimulus components whenever possible, since they work seamlessly even within turbo frames. It’s a compromise, since Stimulus doesn’t allow for easy DOM manipulation, but I’ve been able to make due so far. |
@dvruette thanks the for the response! Stimulus is the goal for me too. Unfortunately we have a lot of random react components that we can't immediately migrate so I was hoping to shim them into turbo frames until we have time to do that. I got the components we were rendering inside of stuff that I moved to a turbo frame to work by doing three things:
I'm not sure if this is the cleanest way to do this but haven't found anything else so far... |
@hbriggs Nice, thanks for letting me know! Good to know that there is a way now, sometimes I'd prefer to use (or reuse) React components, especially when there are lots of DOM manipulations and Stimulus isn't really a good option. Would also be great to get this working out of the box, might take a stab at it if I find the time. |
Got it to work for turbo-frame events: // app/javascript/packs/application.js
var ReactRailsUJS = require('react_ujs')
ReactRailsUJS.handleEvent('turbo:frame-load', ReactRailsUJS.handleMount)
ReactRailsUJS.handleEvent('turbo:frame-render', ReactRailsUJS.handleUnmount) |
After installing react-rails and turbo-rails, I encountered the following problem. Versions and configuration
Problem. 1After implementing useMediaQuery responsive support, the following exception occurs when changing the screen size.
Problem. 2The following error occurs with each page transition.
Hotfix for problem 1The first problem can be avoided by changing the previous snippet as follows:
Hotfix for problem 2The second problem can be avoided by removing the following code:
I don't think this essentially solves the problem, but it solves the glitch anyway. |
In case anyone runs into this on v3+, since components are no longer automatically unmounted here: react-rails/react_ujs/index.js Lines 191 to 196 in 0930f24
I got it to work by calling const componentRequireContext = require.context('components', true)
const ReactRailsUJS = require('react_ujs')
ReactRailsUJS.useContext(componentRequireContext)
// Prevent double mount on page load
ReactRailsUJS._firstTurboLoadSkipped = false
ReactRailsUJS.handleEvent('turbo:load', () => {
if (ReactRailsUJS._firstTurboLoadSkipped) ReactRailsUJS.handleMount()
ReactRailsUJS._firstTurboLoadSkipped = true
})
// Unmount components and call cleanup functions after Turbo navigations
ReactRailsUJS.handleEvent('turbo:before-render', (e) => {
ReactRailsUJS.unmountComponents(e.target)
}) |
I added below lines after going through comments
This causes components to render but components are mounted/rendered multiple times. For example using useEffect hook with empty array dependency is called thrice for me. Is anyone working to add turbo support directly? |
Issue
The next version of Turbolinks, now called Turbo (available for Rails via turbo-rails), is in beta and
react-rails
should be updated to be compatible. The new interface is similar to the old one, the events are just called differently.The relevant events are now called
turbo:load
andturbo:before-render
instead ofturbolinks:load
andturbolinks:before-render
. A new event script along the following lines should do the trick:Along with this some changes to the detection script will be required, but I'm not familiar enough to be able to tell exactly what needs to be added.
Hotfix
I have solved this issue in my app by adding the following two lines to the
application.js
script:For anyone already using Turbo and looking to also use
react-rails
, the above is a hotfix until it's implemented in the package.Thanks to the contributors for taking a look at this.
The text was updated successfully, but these errors were encountered: