Skip to content

Commit 81213f3

Browse files
committed
Merge pull request #234 from shakacode/return-react-element
Make ReactOnRails.render return a reference to the component's backing instance
2 parents ddd07c2 + 599e579 commit 81213f3

File tree

5 files changed

+67
-3
lines changed

5 files changed

+67
-3
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ If you are using [react-rails](https://github.com/reactjs/react-rails) in your p
364364

365365
- Remove the 'react-rails' gem from your Gemfile.
366366

367-
- Remove the generated generated lines for react-rails in your application.js file.
367+
- Remove the generated generated lines for react-rails in your application.js file.
368368

369369
```
370370
//= require react
@@ -386,6 +386,7 @@ Note: If you have components from react-rails you want to use, then you will nee
386386
+ [React Router](docs/additional_reading/react_router.md)
387387
+ [RSpec Configuration](docs/additional_reading/rspec_configuration.md)
388388
+ [Server Rendering Tips](docs/additional_reading/server_rendering_tips.md)
389+
+ [Rails View Rendering from Inline JavaScript](docs/additional_reading/rails_view_rendering_from_inline_javascript.md)
389390
+ [Tips](docs/additional_reading/tips.md)
390391
+ [Tutorial for v2.0](docs/tutorial-v2.md), deployed [here](https://shakacode-react-on-rails.herokuapp.com/).
391392
+ [Turbolinks](docs/additional_reading/turbolinks.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Using ReactOnRails in JavaScript
2+
You can easily render React components in your JavaScript with `render` method that returns a [reference to the component](https://facebook.github.io/react/docs/more-about-refs.html) (virtual DOM element).
3+
4+
```js
5+
/**
6+
* ReactOnRails.render("HelloWorldApp", {name: "Stranger"}, 'app');
7+
*
8+
* Does this:
9+
* ReactDOM.render(React.createElement(HelloWorldApp, {name: "Stranger"}),
10+
* document.getElementById('app'))
11+
*
12+
* @param name Name of your registered component
13+
* @param props Props to pass to your component
14+
* @param domNodeId
15+
* @returns {virtualDomElement} Reference to your component's backing instance
16+
*/
17+
ReactOnRails.render(componentName, props, elementId)
18+
```
19+
20+
## Why do we need this?
21+
Imagine that we have some event with jQuery, it allows us to set component state manually.
22+
23+
```html
24+
<input id="input" type="range" min="0" max="100" />
25+
<div id="root"></div>
26+
27+
<script>
28+
var input = $("#input");
29+
var component = ReactOnRails.render("componentName", { value: input.val() }, "root");
30+
31+
input.on("change", function(e) {
32+
component.setState({ value: input.val() });
33+
});
34+
</script>
35+
```

node_package/src/ReactOnRails.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ ctx.ReactOnRails = {
2929
* @param name Name of your registered component
3030
* @param props Props to pass to your component
3131
* @param domNodeId
32+
* @returns {virtualDomElement} Reference to your component's backing instance
3233
*/
3334
render(name, props, domNodeId) {
3435
const reactElement = createReactElement({ name, props, domNodeId });
35-
ReactDOM.render(reactElement, document.getElementById(domNodeId));
36+
return ReactDOM.render(reactElement, document.getElementById(domNodeId));
3637
},
3738

3839
/**
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* eslint-disable react/no-multi-comp */
2+
import test from 'tape';
3+
import React from 'react';
4+
import ReactOnRails from '../src/ReactOnRails';
5+
import { canUseDOM } from 'fbjs/lib/ExecutionEnvironment';
6+
import JsDom from 'jsdom';
7+
8+
if (!canUseDOM) {
9+
global.document = JsDom.jsdom('<div id="root"></div>');
10+
global.window = document.defaultView;
11+
}
12+
13+
test('ReactOnRails render returns a virtual DOM element for component', (assert) => {
14+
assert.plan(1);
15+
const R1 = React.createClass({
16+
render() {
17+
return (
18+
<div> WORLD </div>
19+
);
20+
},
21+
});
22+
ReactOnRails.register({ R1 });
23+
const actual = ReactOnRails.render('R1', {}, 'root')._reactInternalInstance._currentElement.type;
24+
assert.deepEqual(actual, R1,
25+
'ReactOnRails render should return a virtual DOM element for component');
26+
});

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"react-transform-hmr": "^1.0.1",
3535
"tap-spec": "^4.1.1",
3636
"tape": "^4.4.0",
37-
"webpack": "^1.12.12"
37+
"webpack": "^1.12.12",
38+
"jsdom": "^8.0.0-0"
3839
},
3940
"peerDependencies": {
4041
"react": ">= 0.14",

0 commit comments

Comments
 (0)