Sha256: a3e5dfd44706f9e6e98e01c0cf18b6831990138b3cba404cd60d395c5bc71abf

Contents?: true

Size: 1.38 KB

Versions: 5

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module React
  module Rails
    # A renderer class suitable for `ActionController::Renderers`.
    # It is associated to `:component` in the Railtie.
    #
    # It is prerendered by default with {React::ServerRendering}.
    # Set options[:prerender] to `false` to disable prerendering.
    #
    # @example 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
    class ControllerRenderer
      include React::Rails::ViewHelper
      include ActionView::Helpers::TagHelper
      include ActionView::Helpers::TextHelper

      attr_accessor :output_buffer

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

      # @return [String] HTML for `component_name` with `options[:props]`
      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

      private

      def default_options
        { prerender: true }
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
react-rails-3.2.0 lib/react/rails/controller_renderer.rb
react-rails-3.1.1 lib/react/rails/controller_renderer.rb
react-rails-3.1.0 lib/react/rails/controller_renderer.rb
react-rails-3.0.0 lib/react/rails/controller_renderer.rb
react-rails-3.0.0.rc.0 lib/react/rails/controller_renderer.rb