Migrate From react-rails
In this guide, it is assumed that you have upgraded the react-rails project to use shakapacker version 7. To this end, check out Shakapacker v7 upgrade guide. Upgrading react-rails to version 3 can make the migration smoother but it is not required.
-
Update Deps
- Replace
react-railsinGemfilewith the latest version ofreact_on_railsand runbundle install. - Remove
react_ujsfrompackage.jsonand runyarn install. - Commit changes!
- Replace
-
Run
rails g react_on_rails:installbut do not commit the change.react_on_railsinstalls node dependencies and also creates sample React component, Rails view/controller, and updatesconfig/routes.rb. -
Adapt the project: Check the changes and carefully accept, reject, or modify them as per your project's needs. Besides changes in
config/shakapackerorbabel.configwhich are project-specific, here are the most noticeable changes to address:-
Check Webpack config files at
config/webpack/*. If coming fromreact-railsv3, the changes are minor since you have already made separate configurations for client and server bundles. The most important change here is to notice the different names for the server bundle entry file. You may choose to stick withserver_rendering.jsor useserver-bundle.jswhich is the default name inreact_on_rails. The decision made here affects the other steps. -
In
app/javascriptdirectory you may notice some changes.-
react_on_railsby default usesbundlesdirectory for the React components. You may choose to renamecomponentsintobundlesto follow the convention. -
react_on_railsusesclient-bundle.jsandserver-bundle.jsinstead ofapplication.jsandserver_rendering.js. There is nothing special about these names. It can be set to use any other name (as mentioned above). If you too choose to follow the new names, consider updating the relevantjavascript_pack_tagin your Rails views. -
Update the content of these files to register your React components for client or server-side rendering. Checking the generated files by
react_on_railsinstallation process should give enough hints.
-
-
Check Rails views. In
react_on_rails,react_componentview helper works slightly differently. It takes two arguments: the component name, and options. Props is one of the options. Take a look at the following example:- <%= react_component('Post', { title: 'New Post' }, { prerender: true }) %> + <%= react_component('Post', { props: { title: 'New Post' }, prerender: true }) %>
-
You can also check react-rails-to-react-on-rails branch on react-rails example app for an example of migration from react-rails v3 to react_on_rails v13.4.