Class: React::Rails::ComponentMount
- Inherits:
-
Object
- Object
- React::Rails::ComponentMount
- Includes:
- ActionView::Helpers::TagHelper, ActionView::Helpers::TextHelper
- Defined in:
- lib/react/rails/component_mount.rb
Overview
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`.
Instance Attribute Summary collapse
-
#output_buffer ⇒ Object
Returns the value of attribute output_buffer.
Instance Method Summary collapse
-
#initialize ⇒ ComponentMount
constructor
A new instance of ComponentMount.
-
#react_component(name, props = {}, 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.
-
#setup(controller) ⇒ Object
ControllerLifecycle calls these hooks You can use them in custom helper implementations.
- #teardown(controller) ⇒ Object
Constructor Details
#initialize ⇒ ComponentMount
Returns a new instance of ComponentMount.
14 15 16 |
# File 'lib/react/rails/component_mount.rb', line 14 def initialize @cache_ids = [] end |
Instance Attribute Details
#output_buffer ⇒ Object
Returns the value of attribute output_buffer.
11 12 13 |
# File 'lib/react/rails/component_mount.rb', line 11 def output_buffer @output_buffer end |
Instance Method Details
#react_component(name, props = {}, 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.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/react/rails/component_mount.rb', line 30 def react_component(name, props = {}, = {}, &block) = { :tag => } if .is_a?(Symbol) if .fetch(:camelize_props, camelize_props_switch) props = React.camelize_props(props) end = [:prerender] if block = Proc.new{ concat(prerender_component(name, props, )) } end = .reverse_merge(:data => {}) unless == :static [:data].tap do |data| data[:react_class] = name data[:react_props] = (props.is_a?(String) ? props : props.to_json) data[:hydrate] = 't' if num_components = @cache_ids.count { |c| c.start_with? name } data[:react_cache_id] = "#{name}-#{num_components}" end end html_tag = [:tag] || :div # remove internally used properties so they aren't rendered to DOM .except!(:tag, :prerender, :camelize_props) rendered_tag = content_tag(html_tag, '', , &block) if React::ServerRendering.[:replay_console] # Grab the server-rendered console replay script # and move it _outside_ the container div rendered_tag.sub!(/\n(<script class="react-rails-console-replay">.*<\/script>)<\/(\w+)>$/m,'</\2>\1') rendered_tag.html_safe else rendered_tag end end |
#setup(controller) ⇒ Object
React::Rails::ControllerLifecycle calls these hooks You can use them in custom helper implementations
20 21 22 |
# File 'lib/react/rails/component_mount.rb', line 20 def setup(controller) @controller = controller end |
#teardown(controller) ⇒ Object
24 25 |
# File 'lib/react/rails/component_mount.rb', line 24 def teardown(controller) end |