Migrate From react-rails
This migration is easiest when the app is already on a modern Rails + Shakapacker baseline.
Preflight
Before swapping gems, check these first:
- Webpacker vs Shakapacker: if the app still uses
webpacker, migrate toshakapackerfirst. - Bundler age: some older
react-railsapps still carry Bundler 1.x lockfiles. Those can fail on modern Ruby before you even reach the migration work. - Rails age: current
react_on_railsrequires Rails 5.2+. Rails 5.1 / Webpacker 3 apps are usually a staged migration, not a one-command migration. - Package manager metadata: if you have
yarn.lock,pnpm-lock.yaml, orbun.lock*, ensurepackage.jsonhas a matchingpackageManagerfield (for examplenpm@10.9.2,yarn@1.22.22,pnpm@10.12.1, orbun@1.2.13). - Browserslist source: use one source only. If
.browserslistrcexists, removebrowserslistfrompackage.json. - JSX-in-.js projects: current install generator auto-switches to Babel when JSX is detected in
.jsfiles. If your project has custom transpiler setup, reviewconfig/shakapacker.ymlafter generation.
If you are already on shakapacker 7+ and React 18+, the migration is mostly about helper syntax, component registration, and generated defaults.
If bundle install fails before you even start because the lockfile was generated by Bundler 1.x, refresh the lockfile with a modern Bundler first:
bundle _2.3.26_ lock --update
bundle _2.3.26_ install
If package.json is missing packageManager, set it to your project's actual manager and exact version before running install generators:
# pick the one that matches your lockfile
npm pkg set packageManager='npm@10.9.2'
npm pkg set packageManager='yarn@1.22.22'
npm pkg set packageManager='pnpm@10.12.1'
npm pkg set packageManager='bun@1.2.13'
-
Update Deps
- Replace
react-railsinGemfilewithreact_on_railsand make sureshakapackeris present. - Remove
react_ujsfrompackage.json. - Run
bundle installand your package manager's install command. - Commit changes.
- Replace
-
Run
rails g react_on_rails:installbut do not commit the change.react_on_railsattempts to install node dependencies, creates a sample React component, Rails view/controller, and updatesconfig/routes.rb. If dependency installation fails, the generator prints manual install commands. If required package-manager tooling (Node.js and npm/yarn/pnpm/bun) is unavailable, the generator stops with setup guidance. Run the suggested commands or install missing tools before continuing. -
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 on Shakapacker, the changes are usually localized. The most important difference is the server bundle entrypoint:react-railscommonly usesserver_rendering.js, while React on Rails defaults toserver-bundle.js. -
In
app/javascriptdirectory you may notice some changes.-
react_on_railscan work with manual registration or the newer auto-bundling flow. Auto-bundling is usually the cleaner target for new work. -
react_on_railsusesserver-bundle.jsinstead ofserver_rendering.js. If you keep your old filename, update the generated config accordingly. -
Replace
ReactRailsUJSmounting with explicit React on Rails registration. The generated files show the current registration pattern.
-
-
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 }) %>
-
-
Validate before final cleanup:
-
Confirm that old
react_ujsreferences are gone:rg -n "react_ujs|ReactRailsUJS|server_rendering\.js" app/javascript app/assets app/views config
# or without ripgrep:
grep -rn "react_ujs\|ReactRailsUJS\|server_rendering\.js" app/javascript app/assets app/views config -
Ensure compile succeeds:
bundle exec rails shakapacker:compile -
Review
react_componenthelper calls to ensure they use options-style props:rg -n "react_component\\b" app/views
# or without ripgrep:
grep -rEn "react_component\\b" app/viewsThese commands list candidates only. Inspect each match manually and convert any legacy positional calls (for example
react_component('Post', @props, prerender: true),react_component 'Post', @props,react_component :Post, @props, orreact_component component_name, @props) to options-style props before running tests. -
Run your test suite and fix any app-specific breakages before merging.
-
Legacy compatibility fixes that often make migration one-shot
Older react-rails apps frequently need these additional fixes after the generator run:
-
Remove old UJS mounting from legacy packs (
app/javascript/packs/application.jsand related files).Remove patterns such as:
var componentRequireContext = require.context('components', true);
var ReactRailsUJS = require('react_ujs');
ReactRailsUJS.useContext(componentRequireContext); -
If you are switching to React on Rails
server-bundle.js, remove staleapp/javascript/packs/server_rendering.jsusage. -
Update existing ERB helper calls from old positional props to options-style props:
- <%= react_component 'Post', @props, prerender: true %>
+ <%= react_component('Post', { props: @props, prerender: true }) %> -
If server bundles are not being found, verify
config/initializers/react_on_rails.rbsetup:- On Shakapacker 9.0+, React on Rails usually auto-detects the output path from
private_output_path. Leave this unset unless you intentionally need an override. - On older setups, you may need an explicit value:
config.server_bundle_output_path = "ssr-generated" - On Shakapacker 9.0+, React on Rails usually auto-detects the output path from
-
If
spec/rails_helper.rbgets a malformed merge after generator updates, keep a single validRSpec.configure do |config| ... endblock and include:ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
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.
For a more recent Rails 7-era migration example (published under ShakaCode), see react-on-rails-migration-example, based on ganchdev/react-rails-example.
Practical checklist for Webpacker-era apps
If your app looks like this:
gem "webpacker"inGemfilereact_ujsinpackage.jsonapp/javascript/packs/application.jsapp/javascript/packs/server_rendering.js
then treat the migration as:
- Move from
webpackertoshakapacker. - If the app is still on Rails 5.1, upgrade Rails to 5.2+ before adding current
react_on_rails. - Remove
react_ujs. - Run the React on Rails install generator.
- Replace helper syntax and component registration.
- Review
bin/dev,config/shakapacker.yml, and webpack config diffs before committing.