Skip to content

Commit 5b1b869

Browse files
committed
Upgrade to react_on_rails v2
This commit contains the basic steps needed to migrate from 1.x to 2.0 of react_on_rails. Please see corresponding pull request for more details, as well as the docs for react_on_rails.
1 parent 2f8cf33 commit 5b1b869

18 files changed

+46
-33
lines changed

Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ gem "sdoc", group: :doc
3737
# Use Rails Html Sanitizer for HTML sanitization
3838
gem "rails-html-sanitizer"
3939

40-
gem "react_on_rails"
40+
gem "react_on_rails", "2.0.0.rc.3"
4141

4242
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
4343
gem "therubyracer"

Gemfile.lock

+4-3
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,11 @@ GEM
201201
rainbow (2.0.0)
202202
rake (10.4.2)
203203
rdoc (4.2.0)
204-
react_on_rails (1.2.0)
204+
react_on_rails (2.0.0.rc.3)
205205
connection_pool
206206
execjs (~> 2.5)
207207
rails (>= 3.2)
208+
rainbow (~> 2.0)
208209
ref (2.0.0)
209210
rest-client (1.8.0)
210211
http-cookie (>= 1.0.2, < 2.0)
@@ -346,7 +347,7 @@ DEPENDENCIES
346347
rails-html-sanitizer
347348
rails_12factor
348349
rainbow
349-
react_on_rails
350+
react_on_rails (= 2.0.0.rc.3)
350351
rspec-rails
351352
rspec-retry
352353
rubocop
@@ -364,4 +365,4 @@ DEPENDENCIES
364365
web-console
365366

366367
BUNDLED WITH
367-
1.10.6
368+
1.11.2

app/assets/javascripts/application_dev.js

-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,4 @@
1717
// This one depends on jQuery
1818
//= require turbolinks
1919

20-
// This will soon be removed as it will be in vendor-bundle with react_on_rails 2.0
21-
//= require react_on_rails
22-
2320
//= require rails_startup

app/controllers/pages_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def index
77
# app/views/pages/index.html.erb:20
88
#
99
# <%= react_component('App', render(template: "/comments/index.json.jbuilder"),
10-
# generator_function: true, prerender: true) %>
10+
# prerender: true) %>
1111
#
1212
#
1313
# NOTE: this could be an alternate syntax if you wanted to pass comments as a variable to a partial

app/views/pages/index.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
<%= render "header" %>
44

55
<%= react_component('RouterApp', render(template: "/comments/index.json.jbuilder"),
6-
generator_function: true, prerender: true, raise_on_prerender_error: true) %>
6+
prerender: true, raise_on_prerender_error: true) %>

app/views/pages/no_router.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
<%= render "header" %>
44

55
<%= react_component('App', render(template: "/comments/index.json.jbuilder"),
6-
generator_function: true, prerender: false) %>
6+
prerender: false) %>

app/views/pages/simple.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
</ul>
1010
<hr/>
1111

12-
<%= react_component('SimpleCommentScreen', {}, generator_function: false, prerender: false) %>
12+
<%= react_component('SimpleCommentScreen', {}, prerender: false) %>
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import App from './ClientApp';
22
import RouterApp from './ClientRouterApp';
33
import SimpleCommentScreen from '../components/SimpleCommentScreen/SimpleCommentScreen';
4+
import ReactOnRails from 'react-on-rails';
45

5-
window.App = App;
6-
window.RouterApp = RouterApp;
7-
window.SimpleCommentScreen = SimpleCommentScreen;
6+
ReactOnRails.register(
7+
{
8+
App,
9+
RouterApp,
10+
SimpleCommentScreen,
11+
}
12+
);
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Example of React + Redux
2-
// Shows the mapping from the exported object to the name used by the server rendering.
32
import App from './ServerApp';
43
import RouterApp from './ServerRouterApp';
4+
import ReactOnRails from 'react-on-rails';
55

6-
// We can use the node global object for exposing.
7-
// NodeJs: https://nodejs.org/api/globals.html#globals_global
8-
global.App = App;
9-
global.RouterApp = RouterApp;
6+
ReactOnRails.register(
7+
{
8+
App,
9+
RouterApp,
10+
}
11+
);

client/index.jade

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ html
1111
script(src="vendor-bundle.js")
1212
script(src="app-bundle.js")
1313
script.
14-
ReactDOM.render(RouterApp(!{props}), document.getElementById('app'));
14+
ReactOnRails.render("RouterApp", !{props}, 'app');

client/npm-shrinkwrap.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"react-addons-pure-render-mixin": "^0.14.3",
7070
"react-bootstrap": "^0.28.1",
7171
"react-dom": "^0.14.3",
72+
"react-on-rails": "^2.0.0-rc.3",
7273
"react-redux": "^4.0.0",
7374
"react-router": "^2.0.0-rc4",
7475
"redux": "^3.0.4",

client/webpack.client.base.config.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ module.exports = {
3030
extensions: ['', '.js', '.jsx'],
3131
alias: {
3232
libs: path.join(process.cwd(), 'app', 'libs'),
33+
react: path.resolve('./node_modules/react'),
34+
'react-dom': path.resolve('./node_modules/react-dom'),
3335
},
3436
},
3537
plugins: [
@@ -59,10 +61,6 @@ module.exports = {
5961
{ test: /\.(ttf|eot)$/, loader: 'file' },
6062
{ test: /\.(jpe?g|png|gif|svg|ico)$/, loader: 'url?limit=10000' },
6163

62-
// React is necessary for the client rendering
63-
{ test: require.resolve('react'), loader: 'expose?React' },
64-
{ test: require.resolve('react-dom'), loader: 'expose?ReactDOM' },
65-
{ test: require.resolve('jquery-ujs'), loader: 'imports?jQuery=jquery' },
6664
{ test: require.resolve('jquery'), loader: 'expose?jQuery' },
6765
{ test: require.resolve('jquery'), loader: 'expose?$' },
6866

client/webpack.client.rails.build.config.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ config.entry.vendor.unshift(
2727
// See webpack.common.config for adding modules common to both the webpack dev server and rails
2828

2929
config.module.loaders.push(
30-
{ test: /\.jsx?$/, loader: 'babel-loader', exclude: /node_modules/ },
30+
{
31+
test: /\.jsx?$/, loader: 'babel-loader', exclude: /node_modules/
32+
},
3133
{
3234
test: /\.css$/,
3335
loader: ExtractTextPlugin.extract(
@@ -49,6 +51,10 @@ config.module.loaders.push(
4951
{
5052
test: require.resolve('react'),
5153
loader: 'imports?shim=es5-shim/es5-shim&sham=es5-shim/es5-sham',
54+
},
55+
{
56+
test: require.resolve('jquery-ujs'),
57+
loader: 'imports?jQuery=jquery'
5258
}
5359
);
5460

client/webpack.client.rails.hot.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ config.module.loaders.push(
6767
'sass',
6868
'sass-resources',
6969
],
70+
},
71+
{
72+
test: require.resolve('jquery-ujs'),
73+
loader: 'imports?jQuery=jquery'
7074
}
7175
);
7276

client/webpack.server.rails.build.config.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ module.exports = {
1111
// the project dir
1212
context: __dirname,
1313
entry: [
14-
'react',
15-
'react-dom/server',
14+
'babel-polyfill',
1615
'./app/bundles/comments/startup/serverGlobals',
1716
],
1817
output: {
@@ -49,10 +48,6 @@ module.exports = {
4948
'sass-resources',
5049
],
5150
},
52-
53-
// React is necessary for the client rendering:
54-
{ test: require.resolve('react'), loader: 'expose?React' },
55-
{ test: require.resolve('react-dom/server'), loader: 'expose?ReactDOMServer' },
5651
],
5752
},
5853

config/initializers/react_on_rails.rb

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
# Below options can be overriden by passing to the helper method.
1010
config.prerender = true # default is false
11-
config.generator_function = true # default is false, meaning that you expose ReactComponents directly
1211
config.trace = Rails.env.development? # default is true for development, off otherwise
1312

1413
# For server rendering. This can be set to false so that server side messages are discarded.

docs/jbuilder.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ There's a bunch of gotchas with using [Jbuilder](https://github.com/rails/jbuild
33

44
```erb
55
<%= react_component('App', render(template: "/comments/index.json.jbuilder"),
6-
generator_function: true, prerender: true) %>
6+
prerender: true) %>
77
```
88

99
However, if you try to set the value of the JSON string inside of the controller, then you will run into several issues with rendering the Jbuilder template from the controller. See the notes in this the example code for app/controllers/pages_controller.rb.

0 commit comments

Comments
 (0)