Rendering and mounting
suggest changereact-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="{"name":"John"}"></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:
tag:to use an element other than adivfordata-react-classanddata-react-propsprerender: trueto render the component on the server- any other arguments, such as
class:orid:, passed through tocontent_tag