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
+60-23Lines changed: 60 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,8 @@
9
9
# react-rails
10
10
11
11
12
-
`react-rails` makes it easy 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 (3.2+) application. `react-rails` can:
12
+
`react-rails` makes it easy to use [React](http://facebook.github.io/react/) and [JSX](http://facebook.github.io/react/docs/jsx-in-depth.html)
13
+
in your Ruby on Rails (3.2+) application. `react-rails` can:
13
14
14
15
- Provide [various `react` builds](#reactjs-builds) to your asset bundle
15
16
- Transform [`.jsx` in the asset pipeline](#jsx)
@@ -32,7 +33,8 @@ rails g react:install
32
33
```
33
34
34
35
This will:
35
-
- create a `components.js` manifest file and a `app/assets/javascripts/components/` directory, where you will put your components
36
+
- create a `components.js` manifest file and a `app/assets/javascripts/components/` directory,
37
+
where you will put your components
36
38
- place the following in your `application.js`:
37
39
38
40
```js
@@ -45,7 +47,8 @@ This will:
45
47
46
48
### React.js builds
47
49
48
-
You can pick which React.js build (development, production, with or without [add-ons]((http://facebook.github.io/react/docs/addons.html))) to serve in each environment by adding a config. Here are the defaults:
50
+
You can pick which React.js build (development, production, with or without [add-ons]((http://facebook.github.io/react/docs/addons.html)))
51
+
to serve in each environment by adding a config. Here are the defaults:
49
52
50
53
```ruby
51
54
# config/environments/development.rb
@@ -67,14 +70,40 @@ MyApp::Application.configure do
67
70
end
68
71
```
69
72
70
-
After restarting your Rails server, `//= require react` will provide the build of React.js which was specified by the configurations.
73
+
After restarting your Rails server, `//= require react` will provide the build of React.js which
74
+
was specified by the configurations.
71
75
72
-
`react-rails` offers a few other options for versions & builds of React.js. See [VERSIONS.md](https://github.com/reactjs/react-rails/blob/master/VERSIONS.md) for more info about using the `react-source` gem or dropping in your own copies of React.js.
76
+
`react-rails` offers a few other options for versions & builds of React.js.
77
+
See [VERSIONS.md](https://github.com/reactjs/react-rails/blob/master/VERSIONS.md) for more info about
78
+
using the `react-source` gem or dropping in your own copies of React.js.
73
79
74
80
### JSX
75
81
76
82
After installing `react-rails`, restart your server. Now, `.js.jsx` files will be transformed in the asset pipeline.
77
83
84
+
`react-rails` currently ships with two transformers, to convert jsx code -
85
+
86
+
*`BabelTransformer` using [Babel](http://babeljs.io), which is the default transformer.
87
+
*`JSXTransformer` using `JSXTransformer.js`
88
+
89
+
#### BabelTransformer options
90
+
91
+
You can use babel's [transformers](http://babeljs.io/docs/advanced/transformers/) and [custom plugins](http://babeljs.io/docs/advanced/plugins/),
92
+
and pass [options](http://babeljs.io/docs/usage/options/) to the babel transpiler adding following configurations:
To use CoffeeScript, create `.js.jsx.coffee` files and embed JSX inside backticks, for example:
89
-
90
-
```coffee
91
-
Component=React.createClass
92
-
render:->
93
-
`<ExampleComponent videos={this.props.videos} />`
94
-
```
95
-
96
117
### Rendering & mounting
97
118
98
-
`react-rails` includes a view helper (`react_component`) and an unobtrusive JavaScript driver (`react_ujs`) which work together to put React components on the page. You should require the UJS driver in your manifest after `react` (and after `turbolinks` if you use [Turbolinks](https://github.com/rails/turbolinks)).
119
+
`react-rails` includes a view helper (`react_component`) and an unobtrusive JavaScript driver (`react_ujs`)
120
+
which work together to put React components on the page. You should require the UJS driver
121
+
in your manifest after `react` (and after `turbolinks` if you use [Turbolinks](https://github.com/rails/turbolinks)).
99
122
100
123
The __view helper__ puts a `div` on the page with the requested component class & props. For example:
101
124
@@ -105,9 +128,12 @@ The __view helper__ puts a `div` on the page with the requested component class
On page load, the __`react_ujs` driver__ will scan the page and mount components using `data-react-class` and `data-react-props`. Before page unload, it will unmount components (if you want to disable this behavior, remove `data-react-class` attribute in `componentDidMount`).
131
+
On page load, the __`react_ujs` driver__ will scan the page and mount components using `data-react-class`
132
+
and `data-react-props`. Before page unload, it will unmount components (if you want to disable this behavior,
133
+
remove `data-react-class` attribute in `componentDidMount`).
109
134
110
-
`react_ujs` uses Turbolinks events if they're available, otherwise, it uses native events. __Turbolinks >= 2.4.0__ is recommended because it exposes better events.
135
+
`react_ujs` uses Turbolinks events if they're available, otherwise, it uses native events.
136
+
__Turbolinks >= 2.4.0__ is recommended because it exposes better events.
111
137
112
138
The view helper's signature is:
113
139
@@ -141,16 +167,19 @@ _(It will be also be mounted by the UJS on page load.)_
141
167
142
168
There are some requirements for this to work:
143
169
144
-
-`react-rails` must load your code. By convention it uses `components.js`, which was created by the install task. This file must include your components _and_ their dependencies (eg, Underscore.js).
145
-
- Your components must be accessible in the global scope. If you are using `.js.jsx.coffee` files then the wrapper function needs to be taken into account:
170
+
-`react-rails` must load your code. By convention it uses `components.js`, which was created
171
+
by the install task. This file must include your components _and_ their dependencies (eg, Underscore.js).
172
+
- Your components must be accessible in the global scope.
173
+
If you are using `.js.jsx.coffee` files then the wrapper function needs to be taken into account:
146
174
147
175
```coffee
148
176
# @ is `window`:
149
177
@Component=React.createClass
150
178
render:->
151
179
`<ExampleComponent videos={this.props.videos} />`
152
180
```
153
-
- Your code can't reference `document`. Prerender processes don't have access to `document`, so jQuery and some other libs won't work in this environment :(
181
+
- Your code can't reference `document`. Prerender processes don't have access to `document`,
182
+
so jQuery and some other libs won't work in this environment :(
154
183
155
184
You can configure your pool of JS virtual machines and specify where it should load code:
156
185
@@ -171,7 +200,11 @@ end
171
200
172
201
### Component generator
173
202
174
-
`react-rails` ships with a Rails generator to help you get started with a simple component scaffold. You can run it using `rails generate react:component ComponentName`. The generator takes an optional list of arguments for default propTypes, which follow the conventions set in the [Reusable Components](http://facebook.github.io/react/docs/reusable-components.html) section of the React documentation.
203
+
`react-rails` ships with a Rails generator to help you get started with a simple component scaffold.
204
+
You can run it using `rails generate react:component ComponentName`.
205
+
The generator takes an optional list of arguments for default propTypes,
206
+
which follow the conventions set in the [Reusable Components](http://facebook.github.io/react/docs/reusable-components.html)
207
+
section of the React documentation.
175
208
176
209
For example:
177
210
@@ -222,11 +255,13 @@ The following additional arguments have special behavior:
222
255
*`oneOf` behaves like an enum, and takes an optional list of strings in the form of `'name:oneOf{one,two,three}'`.
223
256
*`oneOfType` takes an optional list of react and custom types in the form of `'model:oneOfType{string,number,OtherType}'`.
224
257
225
-
Note that the arguments for `oneOf` and `oneOfType` must be enclosed in single quotes to prevent your terminal from expanding them into an argument list.
258
+
Note that the arguments for `oneOf` and `oneOfType` must be enclosed in single quotes
259
+
to prevent your terminal from expanding them into an argument list.
226
260
227
261
### Jbuilder & react-rails
228
262
229
-
If you use Jbuilder to pass a JSON string to `react_component`, make sure your JSON is a stringified hash, not an array. This is not the Rails default -- you should add the root node yourself. For example:
263
+
If you use Jbuilder to pass a JSON string to `react_component`, make sure your JSON is a stringified hash,
264
+
not an array. This is not the Rails default -- you should add the root node yourself. For example:
230
265
231
266
```ruby
232
267
# BAD: returns a stringified array
@@ -244,7 +279,9 @@ end
244
279
245
280
## CoffeeScript
246
281
247
-
It is possible to use JSX with CoffeeScript. We need to embed JSX inside backticks so CoffeeScript ignores the syntax it doesn't understand. Here's an example:
282
+
It is possible to use JSX with CoffeeScript. To use CoffeeScript, create files with an extension `.js.jsx.coffee`.
283
+
We also need to embed JSX code inside backticks so that CoffeeScript ignores the syntax it doesn't understand.
ActiveSupport::Deprecation.warn("Setting config.react.jsx_transform_options for :harmony, :strip_types, and :asset_path keys is now deprecated and has no effect with the default Babel Transformer."+
10
+
"Please use new Babel Transformer options :whitelist, :plugin instead.")
0 commit comments