You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+77-13Lines changed: 77 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -7,11 +7,10 @@
7
7
8
8
react-rails is a ruby gem which makes it easier to use [React](http://facebook.github.io/react/) and [JSX](http://facebook.github.io/react/docs/jsx-in-depth.html) in your Ruby on Rails application.
9
9
10
-
This is done in 2 ways:
11
-
12
-
1. making it easy to include `react.js` as part of your dependencies in `application.js`.
13
-
2. transforming JSX into regular JS on request, or as part of asset precompilation.
14
10
11
+
1. Making it easy to include `react.js` as part of your dependencies in `application.js`.
12
+
2. Transforming JSX into regular JS on request, or as part of asset precompilation.
13
+
3. View helpers to render React components in an unobtrusive style and/or on the server.
15
14
16
15
## Installation
17
16
@@ -53,7 +52,18 @@ Alternatively, you can include it directly as a separate script tag:
53
52
54
53
To transform your JSX into JS, simply create `.js.jsx` files, and ensure that the file has the `/** @jsx React.DOM */` docblock. These files will be transformed on request, or precompiled as part of the `assets:precompile` task.
55
54
56
-
### Unobtrusive javascript
55
+
CoffeeScript files can also be used, by creating `.js.jsx.coffee` files. You must use this form of the docblock at the top of each file: `###* @jsx React.DOM ###`. We also need to embed JSX inside backticks so CoffeeScript ignores the syntax it doesn't understand. Here's an example:
56
+
57
+
```coffee
58
+
###* @jsx React.DOM ###
59
+
60
+
Component=React.createClass
61
+
render:->
62
+
`<ExampleComponent videos={this.props.videos} />`
63
+
```
64
+
65
+
66
+
### Unobtrusive JavaScript
57
67
58
68
`react_ujs` will call `React.renderComponent` for every element with `data-react-class` attribute. React properties can be specified by `data-react-props` attribute in JSON format. For example:
59
69
@@ -76,7 +86,7 @@ To use `react_ujs`, simply `require` it after `react` (and after `turbolinks` if
76
86
77
87
### Viewer helper
78
88
79
-
There is a viewer helper method `react_component`. It is designed to work with `react_ujs` and takes React class name, properties, HTML options as arguments:
89
+
There is a viewer helper method `react_component`. It is designed to work with `react_ujs` and takes a React class name, properties, and HTML options as arguments:
React components can also use the same ExecJS mechanisims in Sprockets to execute JavaScript code on the server, and render React components to HTML to be delivered to the browser, and then the `react_ujs` script will cause the component to be mounted. In this way, users get fast initial page loads and search-engine-friendly pages.
98
109
99
-
It is possible to use JSX with CoffeeScript. The caveat is that you will still need to include the docblock. Since CoffeeScript doesn't allow `/* */` style comments, we need to do something a little different. We also need to embed JSX inside backticks so CoffeeScript ignores the syntax it doesn't understand. Here's an example:
110
+
#### ExecJS
111
+
112
+
By default, ExecJS will use node.js in an external process to run JS code. Because we will be executing JS on the server in production, an in-process, high-performance JS VM should be used. Simply add the proper one for your platform to your Gemfile:
113
+
114
+
```ruby
115
+
gem "therubyracer", :platforms => :ruby
116
+
gem "therubyrhino", :platforms => :jruby
117
+
```
118
+
119
+
#### components.js
120
+
121
+
In order for us to render your React components, we need to be able to find them and load them into the JS VM. By convention, we look for a `assets/components.js` file through the asset pipeline, and load that. For example:
122
+
123
+
```sass
124
+
// app/assets/javascripts/components.js
125
+
//= require_tree ./components
126
+
```
127
+
128
+
This will bring in all files located in the `app/assets/components` directory. You can organize your code however you like, as long as a request for `/assets/components.js` brings in a concatenated file containing all of your React components, and each one has to be available in the global scope (either `window` or `global` can be used). For `.js.jsx` files this is not a problem, but if you are using `.js.jsx.coffee` files then the wrapper function needs to be taken into account:
100
129
101
130
```coffee
102
131
###* @jsx React.DOM ###
103
132
104
133
Component=React.createClass
105
134
render:->
106
135
`<ExampleComponent videos={this.props.videos} />`
136
+
137
+
window.Component= Component
107
138
```
108
139
140
+
#### View Helper
141
+
142
+
To take advantage of server rendering, use the same view helper `react_component`, and pass in `:prerender => true` in the `options` hash.
This will return the fully rendered component markup, and as long as you have included the `react_ujs` script in your page, then the component will also be instantiated and mounted on the client.
109
148
110
149
## Configuring
111
150
@@ -126,6 +165,7 @@ end
126
165
```
127
166
128
167
### Add-ons
168
+
129
169
Beginning with React v0.5, there is another type of build. This build ships with some "add-ons" that might be useful - [take a look at the React documentation for details](http://facebook.github.io/react/docs/addons.html). In order to make these available, we've added another configuration (which defaults to `false`).
130
170
131
171
```ruby
@@ -134,13 +174,37 @@ MyApp::Application.configure do
134
174
end
135
175
```
136
176
137
-
### Changing react.js and JSXTransformer.js versions
177
+
### Server Rendering
138
178
139
-
In some cases you may want to have your `react.js`and `JSXTransformer.js` files come from a different release than the one, that is specified in the `react-rails.gemspec`. To achieve that, you have to manually replace them in your app.
179
+
For performance and thread-safety reasons, a pool of JS VMs are spun up on application start, and the size of the pool and the timeout on requesting a VM from the pool are configurable. You can also say where you want to grab the `react.js` code from, and if you want to change the filenames for the components (this should be an array of filenames that will be requested from the asset pipeline and concatenated together.)
140
180
141
-
react-rails at `0.x` requires React at `0.4+`, or `0.5+` or even higher if you need certain add-ons.
181
+
```ruby
182
+
# config/environments/application.rb
183
+
# These are the defaults if you dont specify any yourself
It is possible to use JSX with CoffeeScript. The caveat is that you will still need to include the docblock. Since CoffeeScript doesn't allow `/* */` style comments, we need to do something a little different. We also need to embed JSX inside backticks so CoffeeScript ignores the syntax it doesn't understand. Here's an example:
196
+
197
+
```coffee
198
+
###* @jsx React.DOM ###
199
+
200
+
Component=React.createClass
201
+
render:->
202
+
`<ExampleComponent videos={this.props.videos} />`
203
+
```
204
+
205
+
### Changing react.js and JSXTransformer.js versions
206
+
207
+
In some cases you may want to have your `react.js` and `JSXTransformer.js` files come from a different release than the one, that is specified in the `react-rails.gemspec`. To achieve that, you have to manually replace them in your app.
144
208
145
209
#### Instructions
146
210
@@ -154,4 +218,4 @@ after `config.react.variant`, e.g. you set `config.react.variant = :development`
154
218
If you replace `JSXTransformer.js` in production environment, you have to restart your rails instance,
155
219
because the jsx compiler context is cached.
156
220
157
-
Name of the `JSXTransformer.js` file *is case-sensitive*.
221
+
Name of the `JSXTransformer.js` file *is case-sensitive*.
0 commit comments