Class: React::Rails::ControllerRenderer

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::TagHelper, ActionView::Helpers::TextHelper, ViewHelper
Defined in:
lib/react/rails/controller_renderer.rb

Overview

A renderer class suitable for `ActionController::Renderers`. It is associated to `:component` in the Railtie.

It is prerendered by default with ServerRendering. Set options to `false` to disable prerendering.

Examples:

Rendering a component from a controller

class TodosController < ApplicationController
  def index
    @todos = Todo.all
    render component: 'TodoList', props: { todos: @todos }, tag: 'span', class: 'todo'
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ViewHelper

#react_component

Constructor Details

#initialize(options = {}) ⇒ ControllerRenderer

Returns a new instance of ControllerRenderer


23
24
25
26
# File 'lib/react/rails/controller_renderer.rb', line 23

def initialize(options={})
  controller = options[:controller]
  @__react_component_helper = controller.__react_component_helper
end

Instance Attribute Details

#output_bufferObject

Returns the value of attribute output_buffer


21
22
23
# File 'lib/react/rails/controller_renderer.rb', line 21

def output_buffer
  @output_buffer
end

Instance Method Details

#call(component_name, options, &block) ⇒ String

Returns HTML for `component_name` with `options`

Returns:

  • (String)

    HTML for `component_name` with `options`


29
30
31
32
33
# File 'lib/react/rails/controller_renderer.rb', line 29

def call(component_name, options, &block)
  props = options.fetch(:props, {})
  options = default_options.merge(options.slice(:data, :aria, :tag, :class, :id, :prerender, :camelize_props))
  react_component(component_name, props, options, &block)
end