Module: RailsUtil::ReactHelper

Defined in:
lib/rails_util/react_helper.rb

Overview

RailsUtil::ReactHelper contains wrapper methods for rendering React components using the react_on_rails gem

Instance Method Summary collapse

Instance Method Details

#render_component(component, options = {}) ⇒ Object

Helper method for rendering a React component to the DOM Note: A view file named component.html.erb containing the react_component view helper is also required by this method

Examples:

Render a component

# app/controllers/main_controller.rb
class MainController < ApplicationController
  def app
    render_component 'MainApp', props: {}
  end
end

# app/views/application/component.html.erb
<%= react_component component, options %>

Parameters:

  • component (String)

    the name of the React component to render

  • options (Hash) (defaults to: {})

    any props and other options to pass to the React object



18
19
20
# File 'lib/rails_util/react_helper.rb', line 18

def render_component(component, options = {})
  render 'component', locals: { component: component, options: options }
end