Skip to content

Commit b2c6833

Browse files
committed
Using react_on_rails gem for rendering components on the client
- Paired with @samnang
1 parent 771ce17 commit b2c6833

File tree

5 files changed

+21
-25
lines changed

5 files changed

+21
-25
lines changed

Gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ gem "rails-html-sanitizer"
3737
# Use Unicorn as the app server
3838
gem "unicorn"
3939

40+
gem "react_on_rails", "~> 0.1.1"
41+
4042
gem "autoprefixer-rails"
4143

4244
# Use Capistrano for deployment

Gemfile.lock

+4
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ GEM
172172
raindrops (0.15.0)
173173
rake (10.4.2)
174174
rdoc (4.2.0)
175+
react_on_rails (0.1.1)
176+
execjs (~> 2.5)
177+
rails (~> 4.2)
175178
rspec-core (3.3.2)
176179
rspec-support (~> 3.3.0)
177180
rspec-expectations (3.3.1)
@@ -281,6 +284,7 @@ DEPENDENCIES
281284
rails-html-sanitizer
282285
rails_12factor
283286
rainbow
287+
react_on_rails (~> 0.1.1)
284288
rspec-rails
285289
rubocop
286290
ruby-lint

app/views/pages/index.html.erb

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
</li>
1717
</ul>
1818
<hr/>
19-
<div id="content"></div>
20-
21-
22-
19+
<div id="content">
20+
<%= react_component('App') %>
21+
</div>

client/assets/javascripts/App.jsx

+8-20
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
1-
import $ from 'jquery';
2-
31
import React from 'react';
42
import { Provider } from 'react-redux';
53

64
import CommentScreen from './components/CommentScreen';
75
import CommentStore from './stores/CommentStore';
86

9-
$(function onLoad() {
10-
function render() {
11-
if ($('#content').length > 0) {
12-
React.render((
13-
<Provider store={CommentStore}>
14-
{ () => <CommentScreen /> }
15-
</Provider>
16-
), document.getElementById('content'));
17-
}
18-
}
19-
20-
render();
21-
22-
// Next part is to make this work with turbo-links
23-
$(document).on('page:change', () => {
24-
render();
25-
});
26-
});
7+
window.App = () => {
8+
const reactComponent = (
9+
<Provider store={CommentStore}>
10+
{() => <CommentScreen />}
11+
</Provider>
12+
);
13+
return reactComponent;
14+
};

client/webpack.rails.config.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ config.module.loaders.push(
2525
// Next 2 lines expose jQuery and $ to any JavaScript files loaded after client-bundle.js
2626
// in the Rails Asset Pipeline. Thus, load this one prior.
2727
{test: require.resolve('jquery'), loader: 'expose?jQuery'},
28-
{test: require.resolve('jquery'), loader: 'expose?$'}
28+
{test: require.resolve('jquery'), loader: 'expose?$'},
29+
30+
// React is necessary for the client rendering:
31+
{test: require.resolve('react'), loader: 'expose?React'}
2932
);
3033
module.exports = config;
3134

0 commit comments

Comments
 (0)