Rendering and mounting

suggest change

react-rails includes a view helper, react_component, and an unobtrusive JavaScript driver, react_ujs. They put React components on the page. Require the UJS driver in the manifest after react, and after Turbolinks if you use it.

The helper writes a div with the component class and props:

<%= react_component("HelloMessage", name: "John") %>
<%# becomes: %>
<div data-react-class="HelloMessage" data-react-props="{&quot;name&quot;:&quot;John&quot;}"></div>

On page load, react_ujs scans the page and mounts components from data-react-class and data-react-props.

With Turbolinks 2, components mount on page:change and unmount on page:before-unload. Turbolinks 2.4.0 or newer is recommended because its events are more reliable. Turbolinks 5 uses turbolinks:load and turbolinks:before-render instead.

After an Ajax update, mount manually:

ReactRailsUJS.mountComponents()

The helper's signature is:

react_component(component_class_name, props = {}, html_options = {})

component_class_name is a string naming a global component class. Dots are allowed, as in "MyApp.Header.MenuItem".

props is an object that responds to #to_json, or an already stringified JSON object (for example from Jbuilder).

html_options may include:

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents