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 with ServerRendering.

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.



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

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.



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

def output_buffer
  @output_buffer
end

Instance Method Details

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

Returns Prerendered HTML for ‘component_name` with `options`.

Returns:

  • (String)

    Prerendered HTML for ‘component_name` with `options`



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

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