Skip to content

Commit 7e369d6

Browse files
authored
Merge pull request #1304 from reactjs/rr-to-ror-migration-docs
Documentation for react-rails to react_on_rails migration
2 parents 2d41579 + 7e7cff1 commit 7e369d6

File tree

1 file changed

+93
-34
lines changed

1 file changed

+93
-34
lines changed

README.md

+93-34
Original file line numberDiff line numberDiff line change
@@ -41,40 +41,44 @@ Read the [full review here](https://clutch.co/profile/shakacode#reviews?sort_by=
4141
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
4242
## Contents
4343

44-
- [Get started with Shakapacker](#get-started-with-shakapacker)
45-
- [Component name](#component-name)
46-
- [File naming](#file-naming)
47-
- [Typescript support](#typescript-support)
48-
- [Test component](#test-component)
49-
- [Use with Asset Pipeline](#use-with-asset-pipeline)
50-
- [Custom JSX Transformer](#custom-jsx-transformer)
51-
- [Transform Plugin Options](#transform-plugin-options)
52-
- [React.js versions](#reactjs-versions)
53-
- [View Helper](#view-helper)
54-
- [Custom View Helper](#custom-view-helper)
55-
- [UJS](#ujs)
56-
- [Mounting & Unmounting](#mounting--unmounting)
57-
- [Event Handling](#event-handling)
58-
- [`getConstructor`](#getconstructor)
59-
- [Server-Side Rendering](#server-side-rendering)
60-
- [Configuration](#configuration)
61-
- [JavaScript State](#javascript-state)
62-
- [Custom Server Renderer](#custom-server-renderer)
63-
- [Controller Actions](#controller-actions)
64-
- [Component Generator](#component-generator)
65-
- [Use with JBuilder](#use-with-jbuilder)
66-
- [Camelize Props](#camelize-props)
67-
- [Changing Component Templates](#changing-component-templates)
68-
- [Upgrading](#upgrading)
69-
- [2.7 to 3.0](#27-to-30)
70-
- [2.3 to 2.4](#23-to-24)
71-
- [Common Errors](#common-errors)
72-
- [Getting warning for `Can't resolve 'react-dom/client'` in React < 18](#getting-warning-for-cant-resolve-react-domclient-in-react--18)
73-
- [Undefined Set](#undefined-set)
74-
- [Using TheRubyRacer](#using-therubyracer)
75-
- [HMR](#hmr)
76-
- [Related Projects](#related-projects)
77-
- [Contributing](#contributing)
44+
- [Get started with Shakapacker](#get-started-with-shakapacker)
45+
- [Component name](#component-name)
46+
- [File naming](#file-naming)
47+
- [Typescript support](#typescript-support)
48+
- [Test component](#test-component)
49+
- [Use with Asset Pipeline](#use-with-asset-pipeline)
50+
- [Custom JSX Transformer](#custom-jsx-transformer)
51+
- [Transform Plugin Options](#transform-plugin-options)
52+
- [React.js versions](#reactjs-versions)
53+
- [View Helper](#view-helper)
54+
- [Custom View Helper](#custom-view-helper)
55+
- [UJS](#ujs)
56+
- [Mounting & Unmounting](#mounting--unmounting)
57+
- [Event Handling](#event-handling)
58+
- [`getConstructor`](#getconstructor)
59+
- [Server-Side Rendering](#server-side-rendering)
60+
- [Configuration](#configuration)
61+
- [JavaScript State](#javascript-state)
62+
- [Custom Server Renderer](#custom-server-renderer)
63+
- [Controller Actions](#controller-actions)
64+
- [Component Generator](#component-generator)
65+
- [Use with JBuilder](#use-with-jbuilder)
66+
- [Camelize Props](#camelize-props)
67+
- [Changing Component Templates](#changing-component-templates)
68+
- [Upgrading](#upgrading)
69+
- [2.7 to 3.0](#27-to-30)
70+
- [2.3 to 2.4](#23-to-24)
71+
- [Migrating from `react-rails` to `react_on_rails`](#migrating-from-react-rails-to-react_on_rails)
72+
- [Why to migrate?](#why-to-migrate)
73+
- [Steps to migrate](#steps-to-migrate)
74+
- [Common Errors](#common-errors)
75+
- [Getting warning for `Can't resolve 'react-dom/client'` in React < 18](#getting-warning-for-cant-resolve-react-domclient-in-react--18)
76+
- [Undefined Set](#undefined-set)
77+
- [Using TheRubyRacer](#using-therubyracer)
78+
- [HMR](#hmr)
79+
- [Related Projects](#related-projects)
80+
- [Contributing](#contributing)
81+
- [Supporters](#supporters)
7882

7983
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
8084

@@ -801,6 +805,61 @@ For the vast majority of cases this will get you most of the migration:
801805
- add `import PropTypes from 'prop-types'` (Webpacker only)
802806
- re-run `bundle exec rails webpacker:install:react` to update npm packages (Webpacker only)
803807

808+
## Migrating from `react-rails` to `react_on_rails`
809+
810+
### Why to migrate?
811+
812+
[`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.
813+
814+
| **Feature** | **react-rails** | **react-on-rails** |
815+
| ----------------------- |:---------------:|:------------------:|
816+
| Sprockets |||
817+
| Shakapacker |||
818+
| SSR |||
819+
| SSR with HMR |||
820+
| SSR with React-Router |||
821+
| SSR with Code Splitting |||
822+
| Node SSR |||
823+
| Advanced Redux support |||
824+
| ReScript support |||
825+
| I18n support |||
826+
827+
`react_on_rails` offers better performance and bundle optimizations, especially with the option of getting a subscription to `react_on_rails_pro`.
828+
829+
### Steps to migrate
830+
831+
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.
832+
833+
1. Update Deps
834+
835+
1. Replace `react-rails` in `Gemfile` with the latest version of `react_on_rails` and run `bundle install`.
836+
2. Remove `react_ujs` from `package.json` and run `yarn install`.
837+
3. Commit changes!
838+
839+
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`.
840+
841+
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:
842+
843+
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.
844+
845+
2. In `app/javascript` directory you may notice some changes.
846+
847+
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.
848+
849+
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.
850+
851+
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.
852+
853+
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:
854+
855+
```diff
856+
- <%= react_component('Post', { title: 'New Post' }, { prerender: true }) %>
857+
+ <%= react_component('Post', { props: { title: 'New Post' }, prerender: true }) %>
858+
```
859+
860+
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.
861+
862+
804863
## Common Errors
805864
### Getting warning for `Can't resolve 'react-dom/client'` in React < 18
806865

0 commit comments

Comments
 (0)