class React::Rails::ComponentMount

This is the default view helper implementation. It just inserts HTML into the DOM (see {#react_component}).

You can extend this class or provide your own implementation by assigning it to `config.react.view_helper_implementation`.

Attributes

output_buffer[RW]

Public Instance Methods

react_component(name, props = {}, options = {}, &block) click to toggle source

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.

# File lib/react/rails/component_mount.rb, line 25
def react_component(name, props = {}, options = {}, &block)
  options = {:tag => options} if options.is_a?(Symbol)
  props = camelize_props_key(props) if camelize_props_switch

  html_options = options.reverse_merge(:data => {})
  html_options[:data].tap do |data|
    data[:react_class] = name
    data[:react_props] = (props.is_a?(String) ? props : props.to_json)
  end
  html_tag = html_options[:tag] || :div

  # remove internally used properties so they aren't rendered to DOM
  html_options.except!(:tag)

  content_tag(html_tag, '', html_options, &block)
end
setup(env) click to toggle source

ControllerLifecycle calls these hooks You can use them in custom helper implementations

# File lib/react/rails/component_mount.rb, line 16
def setup(env)
end
teardown(env) click to toggle source
# File lib/react/rails/component_mount.rb, line 19
def teardown(env)
end

Private Instance Methods

camelize_props_key(props) click to toggle source
# File lib/react/rails/component_mount.rb, line 44
def camelize_props_key(props)
  return props unless props.is_a?(Hash)
  props.inject({}) do |h, (k,v)|
    h[k.to_s.camelize(:lower)] = v.is_a?(Hash) ? camelize_props_key(v) : v; h
  end
end