Skip to content

React on rails v2 #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ gem "sdoc", group: :doc
# Use Rails Html Sanitizer for HTML sanitization
gem "rails-html-sanitizer"

gem "react_on_rails"
gem "react_on_rails", "2.0.0.rc.4"

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem "therubyracer"
Expand Down
7 changes: 4 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,11 @@ GEM
rainbow (2.0.0)
rake (10.4.2)
rdoc (4.2.0)
react_on_rails (1.2.0)
react_on_rails (2.0.0.rc.4)
connection_pool
execjs (~> 2.5)
rails (>= 3.2)
rainbow (~> 2.0)
ref (2.0.0)
rest-client (1.8.0)
http-cookie (>= 1.0.2, < 2.0)
Expand Down Expand Up @@ -346,7 +347,7 @@ DEPENDENCIES
rails-html-sanitizer
rails_12factor
rainbow
react_on_rails
react_on_rails (= 2.0.0.rc.4)
rspec-rails
rspec-retry
rubocop
Expand All @@ -364,4 +365,4 @@ DEPENDENCIES
web-console

BUNDLED WITH
1.10.6
1.11.2
3 changes: 0 additions & 3 deletions app/assets/javascripts/application_dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,4 @@
// This one depends on jQuery
//= require turbolinks

// This will soon be removed as it will be in vendor-bundle with react_on_rails 2.0
//= require react_on_rails

//= require rails_startup
2 changes: 1 addition & 1 deletion app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def index
# app/views/pages/index.html.erb:20
#
# <%= react_component('App', render(template: "/comments/index.json.jbuilder"),
# generator_function: true, prerender: true) %>
# prerender: true) %>
#
#
# NOTE: this could be an alternate syntax if you wanted to pass comments as a variable to a partial
Expand Down
2 changes: 1 addition & 1 deletion app/views/pages/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
<%= render "header" %>

<%= react_component('RouterApp', render(template: "/comments/index.json.jbuilder"),
generator_function: true, prerender: true, raise_on_prerender_error: true) %>
prerender: true, raise_on_prerender_error: true) %>
2 changes: 1 addition & 1 deletion app/views/pages/no_router.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
<%= render "header" %>

<%= react_component('App', render(template: "/comments/index.json.jbuilder"),
generator_function: true, prerender: false) %>
prerender: false) %>
2 changes: 1 addition & 1 deletion app/views/pages/simple.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
</ul>
<hr/>

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

window.App = App;
window.RouterApp = RouterApp;
window.SimpleCommentScreen = SimpleCommentScreen;
ReactOnRails.register(
{
App,
RouterApp,
SimpleCommentScreen,
}
);
9 changes: 0 additions & 9 deletions client/app/bundles/comments/startup/serverGlobals.jsx

This file was deleted.

11 changes: 11 additions & 0 deletions client/app/bundles/comments/startup/serverRegistration.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Example of React + Redux
import App from './ServerApp';
import RouterApp from './ServerRouterApp';
import ReactOnRails from 'react-on-rails';

ReactOnRails.register(
{
App,
RouterApp,
}
);
2 changes: 1 addition & 1 deletion client/index.jade
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ html
script(src="vendor-bundle.js")
script(src="app-bundle.js")
script.
ReactDOM.render(RouterApp(!{props}), document.getElementById('app'));
ReactOnRails.render("RouterApp", !{props}, 'app');
4 changes: 4 additions & 0 deletions client/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"react-addons-pure-render-mixin": "^0.14.3",
"react-bootstrap": "^0.28.1",
"react-dom": "^0.14.3",
"react-on-rails": "^2.0.0-rc.4",
"react-redux": "^4.0.0",
"react-router": "^2.0.0-rc4",
"redux": "^3.0.4",
Expand Down
10 changes: 3 additions & 7 deletions client/webpack.client.base.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ module.exports = {
vendor: [
'babel-polyfill',
'jquery',
'react',
'react-dom',
],

// This will contain the app entry points defined by webpack.hot.config and webpack.rails.config
app: [
'./app/bundles/comments/startup/clientGlobals',
'./app/bundles/comments/startup/clientRegistration',
],
},
resolve: {
extensions: ['', '.js', '.jsx'],
alias: {
libs: path.join(process.cwd(), 'app', 'libs'),
react: path.resolve('./node_modules/react'),
'react-dom': path.resolve('./node_modules/react-dom'),
},
},
plugins: [
Expand Down Expand Up @@ -59,10 +59,6 @@ module.exports = {
{ test: /\.(ttf|eot)$/, loader: 'file' },
{ test: /\.(jpe?g|png|gif|svg|ico)$/, loader: 'url?limit=10000' },

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

Expand Down
10 changes: 9 additions & 1 deletion client/webpack.client.rails.build.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ config.entry.vendor.unshift(
// See webpack.common.config for adding modules common to both the webpack dev server and rails

config.module.loaders.push(
{ test: /\.jsx?$/, loader: 'babel-loader', exclude: /node_modules/ },
{
test: /\.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/,
},
{
test: /\.css$/,
loader: ExtractTextPlugin.extract(
Expand All @@ -49,6 +53,10 @@ config.module.loaders.push(
{
test: require.resolve('react'),
loader: 'imports?shim=es5-shim/es5-shim&sham=es5-shim/es5-sham',
},
{
test: require.resolve('jquery-ujs'),
loader: 'imports?jQuery=jquery',
}
);

Expand Down
4 changes: 4 additions & 0 deletions client/webpack.client.rails.hot.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ config.module.loaders.push(
'sass',
'sass-resources',
],
},
{
test: require.resolve('jquery-ujs'),
loader: 'imports?jQuery=jquery',
}
);

Expand Down
9 changes: 2 additions & 7 deletions client/webpack.server.rails.build.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ module.exports = {
// the project dir
context: __dirname,
entry: [
'react',
'react-dom/server',
'./app/bundles/comments/startup/serverGlobals',
'babel-polyfill',
'./app/bundles/comments/startup/serverRegistration',
],
output: {
filename: 'server-bundle.js',
Expand Down Expand Up @@ -49,10 +48,6 @@ module.exports = {
'sass-resources',
],
},

// React is necessary for the client rendering:
{ test: require.resolve('react'), loader: 'expose?React' },
{ test: require.resolve('react-dom/server'), loader: 'expose?ReactDOMServer' },
],
},

Expand Down
1 change: 0 additions & 1 deletion config/initializers/react_on_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

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

# For server rendering. This can be set to false so that server side messages are discarded.
Expand Down
2 changes: 1 addition & 1 deletion docs/jbuilder.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ There's a bunch of gotchas with using [Jbuilder](https://github.com/rails/jbuild

```erb
<%= react_component('App', render(template: "/comments/index.json.jbuilder"),
generator_function: true, prerender: true) %>
prerender: true) %>
```

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.
Expand Down