Module: React::Component::ShouldComponentUpdate

Defined in:
lib/react/component/should_component_update.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#call_needs_update(next_params, next_state) ⇒ Object

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



41
42
43
44
45
46
47
48
49
50
# File 'lib/react/component/should_component_update.rb', line 41

def call_needs_update(next_params, next_state)
  component = self
  next_params.define_singleton_method(:changed?) do
    component.props_changed?(self)
  end
  next_state.define_singleton_method(:changed?) do
    component.native_state_changed?(next_state)
  end
  needs_update?(next_params, next_state)
end

#native_state_changed?(next_state_hash) ⇒ Boolean

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

Returns:



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/react/component/should_component_update.rb', line 69

def native_state_changed?(next_state_hash)
  # next_state = next_state_hash.to_n
  # %x{
  #   var current_state = #{@native}.state
  #   var normalized_next_state =
  #     !next_state || Object.keys(next_state).length === 0 ? false : next_state
  #   var normalized_current_state =
  #     !current_state || Object.keys(current_state).length === 0 ? 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(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-***'])
  # }
  state_hash = Hash.new(`#{@native}.state`)
  next_state_hash != state_hash
end

#props_changed?(next_props) ⇒ Boolean

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

Returns:



93
94
95
96
# File 'lib/react/component/should_component_update.rb', line 93

def props_changed?(next_props)
  props = Hash.new(`#{@native}.props`)
  next_props != props
end

#should_component_update?(next_props, next_state) ⇒ Boolean

Returns:



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/react/component/should_component_update.rb', line 26

def should_component_update?(next_props, next_state)
  State.set_state_context_to(self, false) do
    # rubocop:disable Style/DoubleNegation # we must return true/false to js land
    if respond_to?(:needs_update?)
      !!call_needs_update(next_props, next_state)
    else
      (props_changed?(next_props) || native_state_changed?(next_state))
    end
    # rubocop:enable Style/DoubleNegation
  end
end