Module: React::Rails::ViewHelper

Defined in:
lib/react/rails/view_helper.rb

Instance Method Summary collapse

Instance Method Details

#react_component(name, args = {}, options = {}, &block) ⇒ Object

Render a UJS-type HTML tag annotated with data attributes, which are used by react_ujs to actually instantiate the React component on the client.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/react/rails/view_helper.rb', line 9

def react_component(name, args = {}, options = {}, &block)
  options = {:tag => options} if options.is_a?(Symbol)
  block = Proc.new{concat React::Renderer.render(name, args)} if options[:prerender]

  html_options = options.reverse_merge(:data => {})
  html_options[:data].tap do |data|
    data[:react_class] = name
    data[:react_props] = React::Renderer.react_props(args) unless args.empty?
  end
  html_tag = html_options[:tag] || :div
  
  # remove internally used properties so they aren't rendered to DOM
  html_options.except!(:tag, :prerender)
  
  (html_tag, '', html_options, &block)
end