Module: React::Component::ShouldComponentUpdate

Defined in:
lib/react/component/should_component_update.rb,
lib/reactrb/deep-compare.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, native_next_state) ⇒ Object

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



42
43
44
45
46
47
48
49
50
51
52
# 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
    next_params != props
  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) ⇒ Boolean

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

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


88
89
90
# File 'lib/react/component/should_component_update.rb', line 88

def props_changed?(next_params)
  next_params != props
end

#should_component_update?(native_next_props, native_next_state) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
36
37
# 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