Skip to content

Commit 927a002

Browse files
committed
Add docs for migration to react_on_rails
1 parent 230e3f6 commit 927a002

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,61 @@ For the vast majority of cases this will get you most of the migration:
801801
- add `import PropTypes from 'prop-types'` (Webpacker only)
802802
- re-run `bundle exec rails webpacker:install:react` to update npm packages (Webpacker only)
803803

804+
## Migrating from `react-rails` to `react_on_rails`
805+
806+
### Why to migrate?
807+
808+
[`react_on_rails`](https://github.com/shakacode/react_on_rails/) offers several additional features for a Rails + React application. The following is a table of features comparison.
809+
810+
| **Feature** | **react-rails** | **react-on-rails** |
811+
| ----------------------- |:---------------:|:------------------:|
812+
| Sprockets |||
813+
| Shakapacker |||
814+
| SSR |||
815+
| SSR with HMR |||
816+
| SSR with React-Router |||
817+
| SSR with Code Splitting |||
818+
| Node SSR |||
819+
| Advanced Redux support |||
820+
| ReScript support |||
821+
| I18n support |||
822+
823+
`react_on_rails` offers better performance and bundle optimizations, especially with the option of getting a subscription to `react_on_rails_pro`.
824+
825+
### Steps to migrate
826+
827+
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](https://github.com/shakacode/shakapacker/tree/master/docs/v7_upgrade.md). Upgrading `react-rails` to version 3 can make the migration smoother but it is not required.
828+
829+
1. Update Deps
830+
831+
1. Replace `react-rails` in `Gemfile` with the latest version of `react_on_rails` and run `bundle install`.
832+
2. Remove `react_ujs` from `package.json` and run `yarn install`.
833+
3. Commit changes!
834+
835+
2. 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 update `config/routes.rb`.
836+
837+
3. Adapt the project: Check the changes and carefully accept, reject, or modify them as per your project's needs. Besides changes in `config/shakapacker` or `babel.config` which are project-specific, here are the most noticeable changes to address:
838+
839+
1. Check webpack config files at `config/webpack/*`. If coming from `react-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 with `server_rendering.js` or use `server-bundle.js` which is the default name in `react_on_rails`. The decision made here, affects the other steps.
840+
841+
2. In `app/javascript` directory you may notice some changes.
842+
843+
1. `react_on_rails` by default uses `bundles` directory for the React components. You may choose to rename `components` into `bundles` to follow the convention.
844+
845+
2. `react_on_rails` uses `client-bundle.js` and `server-bundle.js` instead of `application.js` and `server_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 relevant `javascript_pack_tag` in your Rails views.
846+
847+
3. 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.
848+
849+
3. 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:
850+
851+
```diff
852+
- <%= react_component('Post', { title: 'New Post' }, { prerender: true }) %>
853+
+ <%= react_component('Post', { props: { title: 'New Post' }, prerender: true }) %>
854+
```
855+
856+
You can also check [react-rails-to-react-on-rails](https://github.com/shakacode/react-rails-example-app/tree/react-rails-to-react-on-rails) branch on [react-rails example app](https://github.com/shakacode/react-rails-example-app) for an example of migration from `react-rails` v3 to `react_on_rails` v13.4.
857+
858+
804859
## Common Errors
805860
### Getting warning for `Can't resolve 'react-dom/client'` in React < 18
806861

0 commit comments

Comments
 (0)