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-rails
inGemfile
with the latest version ofreact_on_rails
and runbundle install
. - Remove
react_ujs
frompackage.json
and runyarn install
. - Commit changes!
- Replace
-
Run
rails g react_on_rails:install
but do not commit the change.react_on_rails
installs node dependencies and also creates sample react component, Rails view/controller, and updateconfig/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/shakapacker
orbabel.config
which are project-specific, here are the most noticeable changes to address:-
Check webpack config files at
config/webpack/*
. If coming fromreact-rails
v3, 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.js
or useserver-bundle.js
which is the default name inreact_on_rails
. The decision made here, affects the other steps. -
In
app/javascript
directory you may notice some changes.-
react_on_rails
by default usesbundles
directory for the React components. You may choose to renamecomponents
intobundles
to follow the convention. -
react_on_rails
usesclient-bundle.js
andserver-bundle.js
instead ofapplication.js
andserver_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_tag
in 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_rails
installation process should give enough hints.
-
-
Check Rails views. In
react_on_rails
,react_component
view 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.