module React::Component::ShouldComponentUpdate

Note that beginning in 0.9 we will use standard ruby compare on all params further reducing the need for needs_update?

Public Instance Methods

call_needs_update(next_params, native_next_state) click to toggle source

create opal hashes for next params and state, and attach the changed? method to each hash

# File lib/react/component/should_component_update.rb, line 42
def call_needs_update(next_params, native_next_state)
  component = self
  next_params.define_singleton_method(:changed?) do
    component.props_changed?(self)
  end
  next_state = Hash.new(native_next_state)
  next_state.define_singleton_method(:changed?) do
    component.native_state_changed?(native_next_state)
  end
  needs_update?(next_params, next_state)
end
native_state_changed?(next_state) click to toggle source

rubocop:disable Metrics/MethodLength # for effeciency we want this to be one method

# File lib/react/component/should_component_update.rb, line 67
def native_state_changed?(next_state)
  %x{
    var current_state = #{@native}.state
    var normalized_next_state =
      !#{next_state} || Object.keys(#{next_state}).length === 0 || #{nil} == #{next_state} ?
      false : #{next_state}
    var normalized_current_state =
      !current_state || Object.keys(current_state).length === 0 || #{nil} == current_state ?
      false : current_state
    if (!normalized_current_state != !normalized_next_state) return(true)
    if (!normalized_current_state && !normalized_next_state) return(false)
    if (!normalized_current_state['***_state_updated_at-***'] ||
        !normalized_next_state['***_state_updated_at-***']) return(true)
    return (normalized_current_state['***_state_updated_at-***'] !=
            normalized_next_state['***_state_updated_at-***'])
  }
end
props_changed?(next_params) click to toggle source

Do a shallow compare on the two hashes. Starting in 0.9 we will do a deep compare.

# File lib/react/component/should_component_update.rb, line 88
def props_changed?(next_params)
  Component.deprecation_warning(
    "Using shallow incoming params comparison.\n"\
    'Do a require "reactrb/deep-compare, to get 0.9 behavior'
  )
  (props.keys.sort != next_params.keys.sort) ||
    next_params.detect { |k, v| `#{v} != #{@native}.props[#{k}]` }
end
should_component_update?(native_next_props, native_next_state) click to toggle source
# File lib/react/component/should_component_update.rb, line 26
def should_component_update?(native_next_props, native_next_state)
  State.set_state_context_to(self, false) do
    next_params = Hash.new(native_next_props)
    # rubocop:disable Style/DoubleNegation # we must return true/false to js land
    if respond_to?(:needs_update?)
      !!call_needs_update(next_params, native_next_state)
    else
      !!(props_changed?(next_params) || native_state_changed?(native_next_state))
    end
    # rubocop:enable Style/DoubleNegation
  end
end