module React::Component

Attributes

waiting_on_resources[R]

Public Class Methods

deprecation_warning(message) click to toggle source
# File lib/react/component.rb, line 33
def self.deprecation_warning(message)
  @deprecation_messages ||= []
  message = "Warning: Deprecated feature used in #{name}. #{message}"
  unless @deprecation_messages.include? message
    @deprecation_messages << message
    IsomorphicHelpers.log message, :warning
  end
end
included(base) click to toggle source
# File lib/react/component.rb, line 15
def self.included(base)
  base.include(API)
  base.include(Callbacks)
  base.include(Tags)
  base.include(DslInstanceMethods)
  base.include(ShouldComponentUpdate)
  base.class_eval do
    class_attribute :initial_state
    define_callback :before_mount
    define_callback :after_mount
    define_callback :before_receive_props
    define_callback :before_update
    define_callback :after_update
    define_callback :before_unmount
  end
  base.extend(ClassMethods)
end
new(native_element) click to toggle source
# File lib/react/component.rb, line 42
def initialize(native_element)
  @native = native_element
end

Public Instance Methods

_render_wrapper() click to toggle source
# File lib/react/component.rb, line 118
def _render_wrapper
  State.set_state_context_to(self, true) do
    element = React::RenderingContext.render(nil) { render || '' }
    @waiting_on_resources =
      element.waiting_on_resources if element.respond_to? :waiting_on_resources
    element
  end
# rubocop:disable Lint/RescueException # we want to catch all exceptions regardless
rescue Exception => e
  # rubocop:enable Lint/RescueException
  self.class.process_exception(e, self)
end
cache() click to toggle source
# File lib/react/component/props_wrapper.rb, line 70
def cache
  @cache ||= Hash.new { |h, k| h[k] = [] }
end
component_did_mount() click to toggle source
# File lib/react/component.rb, line 59
def component_did_mount
  State.set_state_context_to(self) do
    run_callback(:after_mount)
    State.update_states_to_observe
  end
rescue Exception => e
  self.class.process_exception(e, self)
end
component_did_update(prev_props, prev_state) click to toggle source
# File lib/react/component.rb, line 82
def component_did_update(prev_props, prev_state)
  State.set_state_context_to(self) do
    self.run_callback(:after_update, Hash.new(prev_props), Hash.new(prev_state))
    State.update_states_to_observe
  end
rescue Exception => e
  self.class.process_exception(e, self)
end
component_will_mount() click to toggle source
# File lib/react/component.rb, line 50
def component_will_mount
  IsomorphicHelpers.load_context(true) if IsomorphicHelpers.on_opal_client?
  set_state! initial_state if initial_state
  State.initialize_states(self, initial_state)
  State.set_state_context_to(self) { run_callback(:before_mount) }
rescue Exception => e
  self.class.process_exception(e, self)
end
component_will_receive_props(next_props) click to toggle source
# File lib/react/component.rb, line 68
def component_will_receive_props(next_props)
  # need to rethink how this works in opal-react, or if its actually that useful within the react.rb environment
  # for now we are just using it to clear processed_params
  State.set_state_context_to(self) { self.run_callback(:before_receive_props, Hash.new(next_props)) }
rescue Exception => e
  self.class.process_exception(e, self)
end
component_will_unmount() click to toggle source
# File lib/react/component.rb, line 91
def component_will_unmount
  State.set_state_context_to(self) do
    self.run_callback(:before_unmount)
    State.remove
  end
rescue Exception => e
  self.class.process_exception(e, self)
end
component_will_update(next_props, next_state) click to toggle source
# File lib/react/component.rb, line 76
def component_will_update(next_props, next_state)
  State.set_state_context_to(self) { self.run_callback(:before_update, Hash.new(next_props), Hash.new(next_state)) }
rescue Exception => e
  self.class.process_exception(e, self)
end
define_state(*args, &block) click to toggle source
# File lib/react/component.rb, line 135
def define_state(*args, &block)
  State.initialize_states(self, self.class.define_state(*args, &block))
end
deprecated_params_method(name, *args, &block) click to toggle source
# File lib/react/component/props_wrapper.rb, line 4
def deprecated_params_method(name, *args, &block)
  React::Component.deprecation_warning"Direct access to param `#{name}`.  Use `params.#{name}` instead."
  params.send(name, *args, &block)
end
emit(event_name, *args) click to toggle source
# File lib/react/component.rb, line 46
def emit(event_name, *args)
  params["_on#{event_name.to_s.event_camelize}"].call(*args)
end
props() click to toggle source
# File lib/react/component/props_wrapper.rb, line 74
def props
  component.props
end
render() click to toggle source
# File lib/react/component.rb, line 114
def render
  raise 'no render defined'
end
update_react_js_state(object, name, value) click to toggle source
# File lib/react/component.rb, line 102
def update_react_js_state(object, name, value)
  if object
    name = "#{object.class}.#{name}" unless object == self
    set_state(
      '***_state_updated_at-***' => Time.now.to_f,
      name => value
    )
  else
    set_state name => value
  end
end
value_for(name) click to toggle source
# File lib/react/component/props_wrapper.rb, line 78
def value_for(name)
  self[name].instance_variable_get("@value") if self[name]
end
watch(value, &on_change) click to toggle source
# File lib/react/component.rb, line 131
def watch(value, &on_change)
  Observable.new(value, on_change)
end