-
-
Notifications
You must be signed in to change notification settings - Fork 632
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #234 from shakacode/return-react-element
Make ReactOnRails.render return a reference to the component's backing instance
- Loading branch information
Showing
5 changed files
with
67 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
docs/additional_reading/rails_view_rendering_from_inline_javascript.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Using ReactOnRails in JavaScript | ||
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). | ||
|
||
```js | ||
/** | ||
* ReactOnRails.render("HelloWorldApp", {name: "Stranger"}, 'app'); | ||
* | ||
* Does this: | ||
* ReactDOM.render(React.createElement(HelloWorldApp, {name: "Stranger"}), | ||
* document.getElementById('app')) | ||
* | ||
* @param name Name of your registered component | ||
* @param props Props to pass to your component | ||
* @param domNodeId | ||
* @returns {virtualDomElement} Reference to your component's backing instance | ||
*/ | ||
ReactOnRails.render(componentName, props, elementId) | ||
``` | ||
|
||
## Why do we need this? | ||
Imagine that we have some event with jQuery, it allows us to set component state manually. | ||
|
||
```html | ||
<input id="input" type="range" min="0" max="100" /> | ||
<div id="root"></div> | ||
|
||
<script> | ||
var input = $("#input"); | ||
var component = ReactOnRails.render("componentName", { value: input.val() }, "root"); | ||
input.on("change", function(e) { | ||
component.setState({ value: input.val() }); | ||
}); | ||
</script> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* eslint-disable react/no-multi-comp */ | ||
import test from 'tape'; | ||
import React from 'react'; | ||
import ReactOnRails from '../src/ReactOnRails'; | ||
import { canUseDOM } from 'fbjs/lib/ExecutionEnvironment'; | ||
import JsDom from 'jsdom'; | ||
|
||
if (!canUseDOM) { | ||
global.document = JsDom.jsdom('<div id="root"></div>'); | ||
global.window = document.defaultView; | ||
} | ||
|
||
test('ReactOnRails render returns a virtual DOM element for component', (assert) => { | ||
assert.plan(1); | ||
const R1 = React.createClass({ | ||
render() { | ||
return ( | ||
<div> WORLD </div> | ||
); | ||
}, | ||
}); | ||
ReactOnRails.register({ R1 }); | ||
const actual = ReactOnRails.render('R1', {}, 'root')._reactInternalInstance._currentElement.type; | ||
assert.deepEqual(actual, R1, | ||
'ReactOnRails render should return a virtual DOM element for component'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters